Live data from Hacker News

The Shortest Crashing C Program

llbit.se

51–60 of 94 posts

Re: The Shortest Crashing C Program

#51

> Also, global variables in C are initialized to zero implicitly, so this is equivalent: EDIT: this is wrong, see below. That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value. See: $ valgrind ./a.out ==5118== Memcheck, a memory error detector ==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5118== Using Valgrind-3.8.1…

See my post: https://news.ycombinator.com/item?id=5762363

main will have a value of zero, and 0x600864 will presumably be &main (it's not the initial arbitrary value of main).

Auto variables are left uninitialized so that they don't have to be given a value when they're allocated. It's for efficiency, and it makes the compiler simpler to have this blanket rule rather than have it try to figure out the minimal initializations necessary (which probably isn't even possible). But this ocnsideration doesn't apply to globals or statics, because the initialization can be done at compile time, or (sometimes, in C++) on program startup.

Re: The Shortest Crashing C Program

#52

Earlier quoted context omitted.

But global variables are static.

No, they're not. In fact, if the program used 'static main;' instead, it wouldn't even compile because the 'main' symbol wouldn't be visible by the linker.

Ok, well I agree. I mean global variables are not created dynamically. There is room reserved for them in the data segment which is initialized to 0. Can you give me an example where a global variable isn't initialized to 0? Your valgrind example doesn't say much about the value in the main variable ..

Edit, @deweerdt: ok :)

Re: The Shortest Crashing C Program

#54

Earlier quoted context omitted.

But global variables are static.

No, they're not. In fact, if the program used 'static main;' instead, it wouldn't even compile because the 'main' symbol wouldn't be visible by the linker.

@bnegreve can't reply to your post, but i was mistaken. externally visible symbols are also initialized to 0

Re: The Shortest Crashing C Program

#55
post #47
post #7

It depends on the definition. You can do better than this if you define a valid C program as anything that passes though the C compiler and generates an executable. Behold the zero length program: $ touch a.c $ gcc -c a.c $ ld a.o ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078 $ ./a.out Segmentation fault

Originally I thought I'd skip mentioning compiling empty files because doing so without linking separately `gcc` will refuse to link it. I updated the article with a reference to your comment.

Actually you don't have to link it separately if you don't link against stdlib. See my comment here: https://news.ycombinator.com/item?id=5762578

Re: The Shortest Crashing C Program

#56

> Also, global variables in C are initialized to zero implicitly, so this is equivalent: EDIT: this is wrong, see below. That's wrong. 'static' variables are initialized to zero. Non-static variables are un-initialized, so they have a "random" value. See: $ valgrind ./a.out ==5118== Memcheck, a memory error detector ==5118== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==5118== Using Valgrind-3.8.1…

Actually it is both. In C, variables with static storage duration are zero initialized. Global(variables at file scope) and variables with static linkage (i.e. the static keyword) both of have static storage duration.

That's correct, my bad.

Re: The Shortest Crashing C Program

#57

Earlier quoted context omitted.

But global variables are static.

No, they're not. In fact, if the program used 'static main;' instead, it wouldn't even compile because the 'main' symbol wouldn't be visible by the linker.

Yes they are ! Global variables have static storage duration and are therefore default initialized. Be careful with the word 'static' which does not always correspond to the the keyword static which has several meaning ! When used with a global variable the static keyword has not the same meaning as static storage duration". It only means no external linkage.

Re: The Shortest Crashing C Program

#58

Earlier quoted context omitted.

But global variables are static.

No, they're not. In fact, if the program used 'static main;' instead, it wouldn't even compile because the 'main' symbol wouldn't be visible by the linker.

Both have so-called "static storage duration", which is what influences the initial value. See C99 standard, section 6.2.4 paragraph 4:

"An object whose identifier is declared with external or internal linkage, or with the storage-class specifier `static' has /static storage duration/. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup."

The default initial value of objects with static storage duration is dealt with in 6.7.8 paragraph 10. Basically: pointers set to NULL, non-pointers have all bits reset, aggregates thus recursively.

Re: The Shortest Crashing C Program

#59
post #20

The explanation is not quite correct - execution starts at &main rather than the address given by the value of main. On VC++, at least - well, on my PC anyway - the process halts because the data segment doesn't have the execute bit set. It isn't trying to run code at address 0. (If execution of bytes in the data segment were possible, which I'm sure it used to be, then you'd still likely get a crash, but it's not gu…

No, a ret instruction would probably segfault, depending on the content of the stack. To terminate a program you have to use the corresponding system call. On linux :

mov $1, %eax

int $0x80

Post reply on HN