FROM : Matthew Fahrenbacher
DATE : Thu Nov 07 22:29:01 2002
On Thursday, Nov 07, 2002, at 09:50PM, Andrew Yager <<email_removed>> wrote:
>Hi,
>
>The following code snippet does not appear to function correctly.
>
>#include <stdio.h>
>#define cube(x) x*x*x
>
>int main()
>{
>int x=1;
>int y=2;
>int z=3;
>int* ptr = &z;
>
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>printf("%d\n", cube(*ptr++));
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>
>
>}
>
>--
>
>The preprocess code outputted from cpp is:
>
># 2 "test.c" 2 3
>
>
>int main()
>{
>int x=1;
>int y=2;
>int z=3;
>int* ptr = &z;
>
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>printf("%d\n", *ptr++**ptr++**ptr++);
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>
>
>}
>
>However when compiled with 3Apple Computer, Inc. GCC version 1151, based on
>gcc version 3.1 20020420 (prerelease),2 the following is the output:
>
>3
>-1073742488
>27
>-1073742484
>-1073742484
>
>You can see on examination that this should not work... At least if it does
>work, it shouldn1t work in this way...!!!!
>
>Sorry for the uninformative error report... Someone with more skill at these
>should perhaps do it...
>
>Yours,
>Andrew
What part are you suprised about? The second negative number? Doing this is asking for trouble: *ptr++
It's much cleaner to do this: (*ptr)++
Then my output is:
3
-1073742488
27
4
-1073742484
Now the only interesting thing is that *ptr doesn't get incremented until after this line: printf("%d\n", cube((*ptr)++));
That's interesting, because I believed defines simply do find-replaces and so your code becomes: printf("%d\n", ((*ptr)++)*((*ptr)++)*((*ptr)++));
If you write it out explicitely (like I have), you end up with 3*4*5, like you would expect. But instead, you don't get an increment until after the end of the line, which explains the 4. But that seems odd... it hs something to with the way that gcc deals with defines.
Matt
DATE : Thu Nov 07 22:29:01 2002
On Thursday, Nov 07, 2002, at 09:50PM, Andrew Yager <<email_removed>> wrote:
>Hi,
>
>The following code snippet does not appear to function correctly.
>
>#include <stdio.h>
>#define cube(x) x*x*x
>
>int main()
>{
>int x=1;
>int y=2;
>int z=3;
>int* ptr = &z;
>
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>printf("%d\n", cube(*ptr++));
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>
>
>}
>
>--
>
>The preprocess code outputted from cpp is:
>
># 2 "test.c" 2 3
>
>
>int main()
>{
>int x=1;
>int y=2;
>int z=3;
>int* ptr = &z;
>
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>printf("%d\n", *ptr++**ptr++**ptr++);
>printf("%d\n", *ptr);
>printf("%d\n", ptr);
>
>
>}
>
>However when compiled with 3Apple Computer, Inc. GCC version 1151, based on
>gcc version 3.1 20020420 (prerelease),2 the following is the output:
>
>3
>-1073742488
>27
>-1073742484
>-1073742484
>
>You can see on examination that this should not work... At least if it does
>work, it shouldn1t work in this way...!!!!
>
>Sorry for the uninformative error report... Someone with more skill at these
>should perhaps do it...
>
>Yours,
>Andrew
What part are you suprised about? The second negative number? Doing this is asking for trouble: *ptr++
It's much cleaner to do this: (*ptr)++
Then my output is:
3
-1073742488
27
4
-1073742484
Now the only interesting thing is that *ptr doesn't get incremented until after this line: printf("%d\n", cube((*ptr)++));
That's interesting, because I believed defines simply do find-replaces and so your code becomes: printf("%d\n", ((*ptr)++)*((*ptr)++)*((*ptr)++));
If you write it out explicitely (like I have), you end up with 3*4*5, like you would expect. But instead, you don't get an increment until after the end of the line, which explains the 4. But that seems odd... it hs something to with the way that gcc deals with defines.
Matt






Cocoa mail archive

