Moderately cute

Jan 18, 2009

t1.c:

#include <stdio.h>
int main()
{
    int i = 0;
    printf("%d\n", &i);
    return 0;
}

t2.c:

#include <stdio.h>

int main() { int i=0; int *p; scanf("%d", &i); p = (int *)(i-8); *p = 45; int j; printf("%d\n", j); p += 2; *p = 3; printf("%d\n", i); return 0; }

Following which:

[coelacanth ~ 05:42:15]$ ./t1 | ./t2
45
3

What's cute about this otherwise unremarkable bit of pointer mayhem: the thoughts that eventually culminated in its existence were given their start by The Intentionality of Human Action.

Comments

on 2009-01-19 4:56:42.0, arthegall commented:

The things that people do to their stacks...

But I don't understand -- is the piped input from t1 really necessary? Or, is that the point?

[permalink]


and, further, on 2009-01-19 9:18:13.0, Ben Wolfson commented:

It's part of the point—while the input from t1 in this case is obviously tendentious given how it was generated, that same input could come from anything and be, fortuitously, the address of something t2 can manipulate without segfaulting.

[permalink]


and, further, on 2009-01-20 15:41:33.0, bitchphd commented:

That is not cute. Kitties are cute.

[permalink]


and, further, on 2009-01-22 11:13:01.0, The Modesto Kid commented:

Is stack organization part of the C/C++ language definition, or is this program's output dependent on the compiler you use?

[permalink]


and, further, on 2009-01-22 11:14:52.0, Ben Wolfson commented:

I think it's architecture-dependent but I'm not certain of that.

[permalink]


and, further, on 2009-01-27 3:09:10.0, arthegall commented:

So you're saying, it's like one of them thar trolley problems that you philosophers seem so fond of, but with pointers to the stack instead of people tied to the rails.

I agree with TMK, that the particular behavior may be entirely architecture dependent...

[permalink]