Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> time they leave something undefined, they do so to leave implementations free to use the underlying platform's default behavior

That's implementation defined (more or less) ie teh compiler can do whatever makes mst sense for its implementation.

Undefined means (more or less) that the compiler can assume the behaviour never happens so can apply transforms without taking it into account.

> to allow compilers to use it as an optimization point

That's the main advantage of undefined behaviour ie if you can ignore the usage, you may be able to apply optimisations that you couldn't if you had to take it into account. In the article, for example, GCC eliminated what it considered dead code for a NULL check of a variable that couldn't be NULL according to the C spec.

That's also probably the most frustrating thing about optimisations based on undefined behaviour ie checks that prevent undefined behaviour are removed because the compiler thinks that the check can't ever succeed because, if it did, there must have been undefined behaviour. But the way the developer was ensuring defined behaviour was with the check!



AFAIK, something having undefined behavior in the spec does not prevent an implementation- (platform-)specific behavior being defined.

As to your point about checks being erased, that generally happens when the checks happen too late (according to the compiler), or in a wrong way. For example, checking that `src` is not NULL _after_ memcpy(sec, dst, 0) is called. Or, checking for overflow by doing `if(x+y<0) ...` when x and y are nonnegative signed ints.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: