Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What does this program print?

Name: Anonymous 2011-10-18 14:55


#include <stdio.h>
int main() {
  int x[5];
  printf("%p\n", x);
  printf("%p\n", x+1);
  printf("%p\n", &x);
  printf("%p\n", &x+1);
  return 0;
}


Since none of you use C compilers I know there will be no risk of cheating.

Name: Anonymous 2011-10-18 14:56

uninitialized array
Garbage.

Name: Anonymous 2011-10-18 14:58

>>2
%p
That's address. Learn to use printf().

Name: Anonymous 2011-10-18 15:02

It will probably be something like:
0xdead0
0xdead4
0xdead0
0xdeae4

But of course, that's implementation-dependent.

Name: Anonymous 2011-10-18 15:08

It would print "Satori", but it deletes System 32 before it ever gets that far.

Name: Anonymous 2011-10-18 15:17


0xbfa1b08c
0xbfa1b090
0xbfa1b08c
0xbfa1b0a0


I cheated with ``GNU gcc'', and actually have no idea what this means.

Name: Anonymous 2011-10-18 15:19

5
6
+5
+6

Name: 2011-10-18 15:20

>>6
I got this:
0xbfd87bdc
0xbfd87be0
0xbfd87bdc
0xbfd87bf0

Name: Anonymous 2011-10-18 15:34

It prints the following:


0x23ad51c9
0x2ead51c9
0x2ead5icp
0xreadsicp

Name: Anonymous 2011-10-18 15:38

Since none of you use C compilers I know there will be no risk of cheating.
You're too naïve, >>1-san. I wrote my first ANSI C compiler at the age of 12.

Name: Anonymous 2011-10-18 15:44

This may surprise you, but I invented that meme.

Name: Anonymous 2011-10-18 15:47

%p expects a void *, you dipshit.

Name: Anonymous 2011-10-18 16:26

&x
&x + sizeof(int)
&x
&x + 5 * sizeof(int)

Name: Anonymous 2011-10-19 13:57

the array is in the .bss section so let's say it begins in 0x400000, it could be something like:

0x400010
0x400014
0x400010
0x400014

on systems where pointers are 32 bits

Name: Anonymous 2011-10-19 14:01

>>14
The last address is incorrect.

Name: Anonymous 2011-10-19 14:08

>>14
why would it be in .bss? it should be on the [b]stack[/b].

Name: Anonymous 2011-10-19 14:13

>>14,16
The C standard mentions no stack!

Name: Anonymous 2011-10-19 14:16

>>16
it is non-initiated data of known length, it belongs on the bss, a pointer to it will be stored on stack or register but the value will point to the bss

Name: Anonymous 2011-10-19 14:22

>>17
But how does it recurse then? Terrible!

Name: Anonymous 2011-10-19 14:25

ARM programs recurse just fine without a stack (most of the time)

Name: Anonymous 2011-10-19 15:00

>>19
That's the joke.

Name: Anonymous 2011-10-19 18:13

>>13-sama is the only correct answer so far.

Name: Anonymous 2011-10-19 18:37

>>22
so far
Are we supposed to keep guessing?

Name: Anonymous 2011-10-19 19:37

>>22
Sounds like bullshit. I'd bet it's implementation-defined.

Name: Anonymous 2011-10-19 19:55


x = &x[0]
x + 1 = &x[1]

&x points to the array as a whole and scales by the size of the array.

Name: Anonymous 2011-10-19 20:02

$ ./a.out
0x7fff5fbffa20
0x7fff5fbffa24
0x7fff5fbffa20
0x7fff5fbffa34

Name: Anonymous 2011-10-19 20:12

>>23
There's a more-specific answer which is also correct if you feel like going for it. There's little point though because you can easily tell from the program output people have posted.

>>24
The exact values are platform specific, but it's going to be the same on current 64bit platforms (and 32bit as well) in common use. If you can tell me a compiler that does something differently on amd64 arch I'd love to hear about it. I'll tell you now that any compiler that doesn't agree with >>13 is in violation of the spec.

(The part you probably think is implementation defined isn't, and it's a thing I wish was marked as incorrect in the spec.)

Name: Anonymous 2011-10-19 21:35

Name: Anonymous 2011-10-20 0:02

>>27
Are you saying that the stack can't be aligned arbitrarily by the compiler? Surely the standard doesn't demand that all the locals be packed together like that, that would be silly.

Name: 29 2011-10-20 0:04

I mean the individual values on the stack, not the entire stack as a whole.

Name: Anonymous 2011-10-20 0:44

>>29-30
First, thank you for giving me this opportunity to optimize my quoting.

Second, http://blogs.oracle.com/ksplice/entry/the_ksplice_pointer_challenge#comment-1318975670991

There are some silly things in the C spec, nonsense holdovers from BCPL, the various meanings of static, some would argue const shenanigans (I'm not one of them, but I acknowledge it's less effective than intended... I certainly wouldn't go overboard and ban type qualifiers from my language.)

Name: Anonymous 2011-10-20 0:57

>>31
Yes, but I mean is &x+1 really guaranteed to point immediately after the end of x? Isn't it allowed for there to be padding after x to allow x+1 to be aligned? Is it even guaranteed that x+1 won't be temporarily stored somewhere totally different in memory, separate from the stack?

Name: Anonymous 2011-10-20 1:15

>>31
When quoting only two posts please refrain from using the range operator `-' and use the list operator `,' instead, as this will result in less space taken up by the operator on non-monospace fonts, thank you.

Name: Anonymous 2011-10-20 1:31

>>32
You seem to have used &x+1 and x+1 interchangeably. You'll have to clarify.

>>33
IHBTAL.

Name: Anonymous 2011-10-20 1:56

>>34
Forget about the array, I think his question is whether int x[5] might in fact end up taking sizeof(int)*8 for example.

The way I see it, int x[5] being an array and not an struct, it's not allowed, ergo your right.

Name: Anonymous 2011-10-20 2:03

>>35
Forget about the array
Here open the gates of hell.

The way I see it, int x[5] being an array and not an struct, it's not allowed
Here open the gates of heaven.

Name: Anonymous 2011-10-20 2:28

Here's what it printed on my DeathStation 9000 before the demons flew out of my nose.
DATPTR:POINTER TO AUTO INT 'x[0]' IN 'main' INVOCATION 0
DATPTR:POINTER TO AUTO INT 'x[1]' IN 'main' INVOCATION 0
SPCPTR:POINTER TO AUTO ARRAY OF 5 INT 'x' IN 'main' INVOCATION 0
MCHCHK: YA DUN GOOF'D ON LINE 7
>2011
>EXCEEDING ALLOCATED PROGRAM AUTOMATIC STORAGE AREA
I SERIOUSLY HOPE YOU GUYS DON'T DO THIS.
SYSTEM HALTED.

Name: Anonymous 2011-10-20 3:53

>>22
I'm >>4 and I was the first one to have the correct offsets without even running the program.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List