Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

I was called a liar when I showed my output (while debugging a problem). So I thought I'll make it into a puzzle.

The output of this code

...
T a,b,c, d;
printf("%p %p %p %p %d %d %d %d %d", &a, &b, &c, &d, &a-&b, &b-&c, &c-&d, &b-&d, &a-&d);
...

Is this

0xbfe1327c 0xbfe13278 0xbfe132ac 0xbfe13274 1 -13 14 1 2

Why is the address of c so weird?

There is absolutely no cheating with T, in fact its defined as struct T {void* v; };

share|improve this question

closed as off topic by dmckee Nov 10 '12 at 12:21

Questions on Programming Puzzles & Code Golf Stack Exchange are expected to relate to programming puzzle or code golf within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What's weird about it? Just because the compiler didn't allocate c next to a,b, and d in the stack frame doesn't make it weird. The compiler probably allocates variables is some arbitrary order (by the order they appear in some compile-time hash table, maybe). What other variables are there in the function? –  Keith Randall Nov 10 '12 at 6:32
    
@KeithRandall: Only those 4. I can also choose which variable has a weird address. It involves one line inside of this function (which is hidden atm) –  acidzombie24 Nov 10 '12 at 6:49
    
Related to meta.stackexchange.com/q/155247/2509 and meta.stackexchange.com/q/155238/2509; and utterly inappropriate for CodeGolf.SE. Learning to construct your own complete, compact, compilable example will help you become a better debugger. –  dmckee Nov 10 '12 at 12:28
    
@dmckee: fyi i already fixed the code, every post i made had info to see the actual problem and all the code I written was standard C++ and I already knew the issue was stack related while everyone thought it was something else. The problem was a C optimization which didnt like calling C ++ code. I had to add data to the generated C files to fix it. –  acidzombie24 Nov 10 '12 at 18:16

1 Answer 1

up vote 1 down vote accepted

Not exactly the same, but a similar effect:

typedef struct T {void* v; } T;
main(){
#define printf { T c; printf

    T a,b,c, d;
    printf("%p %p %p %p %d %d %d %d %d", &a, &b, &c, &d, &a-&b, &b-&c, &c-&d, &b-&d, &a-&d);

    (void)x;(void)y;
}   return 0;
}

Output:

0xbfd4034c 0xbfd40348 0xbfd40340 0xbfd40344 1 2 -1 1 
share|improve this answer
    
Good enough. I accepted. Here is the solution I came up with ideone.com/maumcD –  acidzombie24 Nov 10 '12 at 18:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.