Skip navigation.
 
mlRe: GCC not doing the correct thing?
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

Related mailsAuthorDate
mlGCC not doing the correct thing? Andrew Yager Nov 7, 21:53
mlRe: GCC not doing the correct thing? Matthew Fahrenbach… Nov 7, 22:29
mlRe: GCC not doing the correct thing? Cameron Hayne Nov 7, 22:41
mlRe: GCC not doing the correct thing? Stéphane Sudre Nov 8, 02:35
mlRe: GCC not doing the correct thing? Richard Cleis Nov 8, 10:46
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 8, 10:53
mlRe: GCC not doing the correct thing? Richard Cleis Nov 8, 12:40
mlRe: GCC not doing the correct thing? Domain Administrat… Nov 8, 12:54
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 8, 13:08
mlRe: GCC not doing the correct thing? Nat! Nov 8, 20:23
mlRe: GCC not doing the correct thing? Andrew Yager Nov 8, 20:29
mlRe: GCC not doing the correct thing? Domain Administrat… Nov 9, 22:35
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 07:13
mlRe: GCC not doing the correct thing? Thomas Castiglione Nov 11, 13:21
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 15:21
mlRe: GCC not doing the correct thing? Keith Ray Nov 11, 15:43
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 15:46
mlRe: GCC not doing the correct thing? Clark S. Cox III Nov 11, 16:12
mlRe: GCC not doing the correct thing? Cameron Hayne Nov 11, 23:31
mlRe: GCC not doing the correct thing? Thomas Castiglione Nov 12, 11:48