Live data from Hacker News

I Do Not Know C: Short quiz on undefined behavior (2015)

kukuruku.co

1–10 of 189 posts

Re: I Do Not Know C: Short quiz on undefined behavior (2015)

#3
IMHO the problem is with compilers (and their developers) who think UB really means they can do anything, when what programmers usually expect is, and the standard even notes for one of the possible interpretations of UB, "behaving during translation or program execution in a documented manner characteristic of the environment".

Related reading:

http://blog.metaobject.com/2014/04/cc-osmartass.html

http://blog.regehr.org/archives/1180 and https://news.ycombinator.com/item?id=8233484

Re: I Do Not Know C: Short quiz on undefined behavior (2015)

#4
1. Unless C's variable definition rules are completely different from C++'s, int i; is a full definition, not a declaration. If both definitions appear at the same scope (e.g. global), this will cause either a compiler error or a linker error. A variable declaration would be extern int i;

Re: I Do Not Know C: Short quiz on undefined behavior (2015)

#6
post #4

1. Unless C's variable definition rules are completely different from C++'s, int i; is a full definition, not a declaration. If both definitions appear at the same scope (e.g. global), this will cause either a compiler error or a linker error. A variable declaration would be extern int i;

C's variable definition rules are different from C++'s. gcc happily compiles those two lines, g++ exits with the "redefinition" error.

Re: I Do Not Know C: Short quiz on undefined behavior (2015)

#7

IMHO the problem is with compilers (and their developers) who think UB really means they can do anything , when what programmers usually expect is, and the standard even notes for one of the possible interpretations of UB, "behaving during translation or program execution in a documented manner characteristic of the environment". Related reading: http://blog.metaobject.com/2014/04/cc-osmartass.html http://blog.regehr…

There's no compiler writers throwing out

  if(undefined_behavior) {
    ruin_developers_day();
  }
It tends to be the effects of valid by the spec optimizations making assumptions that would only not be true during undefined behavior.

Re: I Do Not Know C: Short quiz on undefined behavior (2015)

#9
post #4

1. Unless C's variable definition rules are completely different from C++'s, int i; is a full definition, not a declaration. If both definitions appear at the same scope (e.g. global), this will cause either a compiler error or a linker error. A variable declaration would be extern int i;

C's variable definition rules are different from C++'s. gcc happily compiles those two lines, g++ exits with the "redefinition" error.

That was unexpected.

Re: I Do Not Know C: Short quiz on undefined behavior (2015)

#10
post #9

Earlier quoted context omitted.

C's variable definition rules are different from C++'s. gcc happily compiles those two lines, g++ exits with the "redefinition" error.

That was unexpected.

Said every C programmer ever!
Post reply on HN