Live data from Hacker News

The Shortest Crashing C Program

llbit.se

71–80 of 94 posts

Re: The Shortest Crashing C Program

#71
post #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 initial…

> the initialization can be done at compile time, or (sometimes, in C++) on program startup

With ELF binaries for C programs it's done at startup as well. The data segment is created as having memory size SIZEM and file size SIZEF. If SIZEF < SIZEM, memory from SIZEF to SIZEM is set to 0.

Re: The Shortest Crashing C Program

#73
post #34
post #9

Earlier quoted context omitted.

Wordpress strikes again; "error establishing database connection"

A bad hosting strikes again.

I have bad hosting with good software. Ran flawlessly with 8-15ms generation times on #3 of the HN homepage for a couple hours, only the network latency went up to at peak ~1.2 seconds (got less than 1mbps upload here). The page also executes multiple database queries for each pageload, just like Wordpress. No caching needed for me, it's all about optimization.

Re: The Shortest Crashing C Program

#75

Interesting. We tried to do the same for Haskell. The shortest we could come up with: import Unsafe.Coerce;main=unsafeCoerce()1

Why not: main=undefined

That throws an exception, I believe. We wanted something that actually segfaults.

Re: The Shortest Crashing C Program

#76
post #5

I'm not convinced this is a C89 program. It is only an "accident" that the linker doesn't know about types. I find it hard to believe that the C89 spec states that an integer called "main" is to be considered the main function, and suspect this is undefined behaviour (though I've not checked).

It can't be a valid C89 program. On many Harvard architecture based microprocessors data pointers and code pointers have differing size.

Is any crashing C program valid? You can only crash by invoking undefined behavior, and I believe that any program which invokes undefined behavior is "invalid". It's a major pitfall of C that determining "validity" requires solving the halting problem.

Re: The Shortest Crashing C Program

#77
post #72

Who says it will crash? Could run very nicely, printing a list of prime numbers, or write poetry, or anything else that undefined behaviour encompasses.

"global variables in C are initialized to zero implicitly"

NULL pointers will lead to a crash. It would be more interesting to have it as a random pointer, which could do quite anything.

Re: The Shortest Crashing C Program

#79
post #77
post #72

Who says it will crash? Could run very nicely, printing a list of prime numbers, or write poetry, or anything else that undefined behaviour encompasses.

"global variables in C are initialized to zero implicitly" NULL pointers will lead to a crash. It would be more interesting to have it as a random pointer, which could do quite anything.

[deleted]
Post reply on HN