3 ms·
Interestingly, . . . it's a pointless exercise delving into ASM to "see what happens" when you have undefined behaviour in C code. A slight flag change and th
by defrost 14y ago
Interestingly, . . . it's a pointless exercise delving into ASM to "see what happens" when you have undefined behaviour in C code.
A slight flag change and the only code produced could equally well just be a single NOP.
- simias 14y agoStill, gcc is a pretty serious piece of software (no more starting nethack these days), so while the behaviour is undefined it's still interesting to see why the compiler ended up generating a code that behaves so weirdly. If you need to find a use for it, it might make it easier to spot an undefined behaviour later and finding its cause ("hey, this evalutates both to true and false, it must be an unitialized bool!"). So it's still interesting and might not be completely pointless.
- defrost 14y agoLLVM is more interesting piece of software, gcc is kind of crusty and warty. Still, I'm glad you're interested in the language, if you're interested in undefined behaviour then you should carefully and closely read the latest free draft standard (N1570) and perhaps implement a small C compiler of your own ( Appel : Modern Compiler Implementation ). There are many C compilers, they are free to do whatever they want when behaviour is undefined and in many cases you'll unfortunately not see anything odd at all, not until you port your code, change your flags or upgrade the compiler and then you'll having a seething mass of "well this stuff worked when we tested it" errors. Strapping yourself into a vehicle and driving off a cliff teaches you a little about the dangers of accidents and gives you a reason to drive safely, it doesn't particularly teach much about the practice of safe driving. The example code drove off the road, what happened next really isn't all that exciting. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.cs.princeton.edu/~appel/modern/c/ http://www.cs.princeton.edu/~appel/modern/c/
- dllthomas 14y agoMany serious pieces of software are crusty and warty.
- Niten 14y agoshrug I do think it's interesting to find out, in concrete terms, the sort of real-world assumptions that a compiler might make, which make C's undefined behavior what it is. Yes, pointless in terms of fixing the actual program, though... to each his own :)
- mikeash 14y agoIt's not pointless at all. Yes, you can't rely on the output being that way all the time, but it still teaches you about the compiler, the language implementation, and the CPU architecture to investigate this stuff.