Sure, but then you deserve what you get...
Posted May 24, 2011 10:05 UTC (Tue) by
khim (subscriber, #9252)
In reply to:
It's different viewpoints... by etienne
Parent article:
What Every C Programmer Should Know About Undefined Behavior #3/3
Same for debugging, I may want to display some binary values as unsigned in hexadecimal even if I know that is a float (so a dirty cast), to see if I had a memory overflow and in fact this float contains a string...
Right, but how often you actually do that? Do you really want to punish the compiler and force it to keep "for loop iterative variable" in memory to make sure it's changed via float if the pointers are mixed just right?
My favorite example is bug 33498. It's this piece of code:
void table_init(int *value)
{
int i;
int val = 0x03020100;
for (i = 0; i < 256/4; i++) {
value[i] = val;
val += 0x04040404;
}
}
What does this piece of code do? Most people answer: it fills the table. Smart people answer: it efficiently fills the table of chars using trick with type conversions. At that point I show what it really does: it destroys your program. Oops? Nasty bug in the compiler? Nope: Status: RESOLVED INVALID.
The problem with undefined behavior today is not that compilers peruse it for optimizations. It's the fact that they do it so carefully. That means that a lot of cases which should trigger undefined behavior don't blow up but silently work - this means people become complacent. Hopefully LTO will help: it'll allow the compiler to weed the undefined behavior branches more aggressively and this will mean people will be punished earlier and learn to look for undefined behavior cases.
(
Log in to post comments)