Live data from Hacker News

On C-optimizing compilers removing code that has undefined behavior

yodaiken.com

1–10 of 71 posts

Re: On C-optimizing compilers removing code that has undefined behavior

#7
post #5

> The programmer has clearly attempted to set x[0]=0 Then why not just write it like that? This is like deliberately shooting yourself in the foot and complaining that your shotgun works.

Exactly. A compiler cannot guess what a programmer intended, but has to stick to the language semantics.

Also, the article goes on to argue that uninitialized memory is used to seed random generators, which is generally undefined behavior. I don't think this should be a valid use-case.

Re: On C-optimizing compilers removing code that has undefined behavior

#9
post #5

> The programmer has clearly attempted to set x[0]=0 Then why not just write it like that? This is like deliberately shooting yourself in the foot and complaining that your shotgun works.

While I fully agree with you, but I will still like C to be as self explanatory as possible. C is the closest language for which one doesn't need to know the standard to understand it, at least the standardised C. I think it should be one of the biggest aim of C to let the language as it is(except the standard library). This change creates something that might not be expected by some. If this is replaced by an compilation error, I am okay with it.

Re: On C-optimizing compilers removing code that has undefined behavior

#10
post #3

What CPUs in common use still have trap representations? Are there other reasons why reading from an indeterminate value can't give an arbitrary but well-defined result?

x86, in the form of signalling NaNs, when the appropriate fpu flag is set. Some languages feel it's better to fail a computation early and loudly than to silently proceed with a cascade of non-signalling nans possibly getting as far as the screen or database. Delphi for one. C will see this if it's in a library linked with code in such a language.
Post reply on HN