Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 02 Jun 2012 07:15:17 -0500 Message-ID: <4FCA03D2.4AB8@mindspring.com> Date: Sat, 02 Jun 2012 08:15:14 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Q: Local variables initialization shortcut. References: <5835baa1-db3f-4b81-a9e9-3ea20a632ba8@w24g2000vby.googlegroups.com> <4f89a691-f680-4dbc-bdbc-363450ccc001@b26g2000vbt.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 113 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.222.79 X-Trace: sv3-IaSW/NOS3ME2yEhwoM2qH3ZqaNKA7VXgRPsHbzxdKLdmLQoSWbFzIjlgCM+qC235ri3yomVxxXi6qww!XZVmtoWuov7zPdlif87cevA6gAZ4JBvS8jxPjIvoVJlFzqDdwtMwfH6Vlr1KwTHwHZ+14p1WmrXl!q7BbeQgvyA== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5084 Xref: csiph.com comp.lang.c:21665 Jens Thoms Toerring wrote: > > Jean-Christophe <5.d@free.fr> wrote: > > On 2 juin, 12:31, j...@toerring.de : > > > '&z - &a' isn't legal - you can only subtract addresses > > > of locations within a single object (e.g. an array), > > > I've done it many times and it always worked fine : > > &a is the address of 'a' so it's its memory location. > > Well, you were lucky. The standard doesn't define what > the result of subtracting pointers not within the same > object is (or, in the case of arrays, one beyond the > end of the array), so you are relying on whatever your > compiler makes out of it. Some other compiler might > give you a different result. > > > Except for this actual example (when variables are stored > > in stack w/ their adresses allocated by the compiler) > > There also is no requirement for a stack in the C stan- > dard. Most (or probably all) compilers will employ > something stack-like for local variables, but that is > a decision the compiler writer makes, not something > prescribed by the standard. > > > if the location of 'a' thru 'z' are well stored > > (i.e, like in an array) then the following code works fine : > > unsigned int size = (unsigned int)( &z - &a ); > > Yes, it may seem to work on your system with the specific > compiler you are using. If you want your program to work > only on that system and with that compiler then this is > probably all fine, just dont expect it to work anymore > under different circumstances. > > > > everything else is undefined behaviour, And even if > > > disregarding this and assuming that 'a' is at the lowest > > > and 'z' at the highest address, '&z - &a' will be the > > > number of variable between 'a' and 'z' (25) and not the > > > number of bytes used in between, so you would have to > > > multiply by the size of a double. > > > Hum, no : if &z is the highest address > > and &a is the lowest address then (&z - &a) > > is the number of bytes (less one sizeof) and not the > > number of variables (unless 'a' and 'z' are chars) > > That may well be the case. Since you are relying on un- > defined behaviour everything you get is "right". Just for > the fun of it: on my system (x86-64 Linux and with gcc) > the output of this program > > #include > int main( void ) > { > double a, b; > printf( "%d\n", ( int ) ( &a - &b ) ); > return 0; > } > > is -1. So obviously at least some of the deductions you > were led to make by the behaviour on your system aren't > valid from what I see on my machine. And that's quite > ok since the behaviour of that program isn't defined, > so whatever the output is is correct. (And I have de- > finitely worked with machines were variables defined > later had lower addresses). > > For even more giggles take this program: > > #include > int main( void ) > { > double a, b, c; > printf( "%d\n", ( int ) ( &a - &c ) ); > return 0; > } > > The output on my machine is still -1... > > But I am a bit puzzled - I would expect the difference > between two pointers of the same type to be number of > variables of that type in between, not the number of > bytes. Otherwise the behaviour (while undefined) would > be even more weird on your machine since e.g. for > arrays you can rely on the difference between the > addresses of two of its elements to be the number of > elements in between. But then, again, the moment you > do something undefined everything can happen. On the machine that I am using, the following program outputs a pair of 1's. #include int main( void ) { { double a, b; printf( "%d\n", ( int ) ( &a - &b ) ); } { double b, a; printf( "%d\n", ( int ) ( &a - &b ) ); } return 0; } -- pete