“is not required” == undefined behavior?

The wording has changed in various editions of the C++ standard, and in the recent draft cited in the question. (See my comments on the question for the gory details.) C++11 says: Other pointer comparisons are unspecified. C++17 says: Otherwise, neither pointer compares greater than the other. The latest draft, cited in the question, says: … Read more

Are functions calls in a constructor’s initializer-list sequenced?

So I am wondering if it is guaranteed that t.a == 0 and t.b == 1? This will always be true so long as a comes before b in the class declaration and nothing else calls f() between the initialization of a and b. Class members are initialized in the order they are declared in … Read more

Flexible array members can lead to undefined behavior?

The Short Answer Yes. Common conventions of using FAMs expose our programs to the possibility of undefined behavior. Having said that, I’m unaware of any existing conforming implementation that would misbehave. Possible, but unlikely. Even if we don’t actually reach undefined behavior, we are still likely to fail strict conformance. No. The offset of the … Read more

Is this undefined C behaviour?

The output is likely to be 2 in every reasonable case. In reality, what you have is undefined behavior though. Specifically, the standard says: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read … Read more

Why is out-of-bounds pointer arithmetic undefined behaviour?

That’s because pointers don’t behave like integers. It’s undefined behavior because the standard says so. On most platforms however (if not all), you won’t get a crash or run into dubious behavior if you don’t dereference the array. But then, if you don’t dereference it, what’s the point of doing the addition? That said, note … Read more

Benefit of endless-loops without side effects in C++ being undefined behavior compared to C?

The reasons are optimizations only. If the compiler can assume that all loops without side effects terminate, it does not have to prove that. If the non-terminating loops were allowed, the compiler would be allowed to perform certain optimizations only if it could prove termination, that is impossible in general so it would turn into … Read more

When does invoking a member function through a null pointer result in undefined behavior?

Both (a) and (b) result in undefined behavior. It’s always undefined behavior to call a member function through a null pointer. If the function is static, it’s technically undefined as well, but there’s some dispute. The first thing to understand is why it’s undefined behavior to dereference a null pointer. In C++03, there’s actually a … Read more

What are sequence points, and how do they relate to undefined behavior?

C++98 and C++03 This answer is for the older versions of the C++ standard. The C++11 and C++14 versions of the standard do not formally contain ‘sequence points’; operations are ‘sequenced before’ or ‘unsequenced’ or ‘indeterminately sequenced’ instead. The net effect is essentially the same, but the terminology is different. Disclaimer : Okay. This answer … Read more