May I treat a 2D array as a contiguous 1D array?

Both lines do result in undefined behavior. Subscripting is interpreted as pointer addition followed by an indirection, that is, a[0][1234]/p[1234] is equivalent to *(a[0] + 1234)/*(p + 1234). According to [expr.add]/4 (here I quote the newest draft, while for the time OP is proposed, you can refer to this comment, and the conclusion is the … Read more

A question about union in C – store as one type and read as another – is it implementation defined?

This is undefined behaviour. u.i and u.ch are located at the same memory address. So, the result of writing into one and reading from the other depends on the compiler, platform, architecture, and sometimes even compiler’s optimization level. Therefore the output for u.i may not always be 515. Example For example gcc on my machine … 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

Difference between Undefined Behavior and Ill-formed, no diagnostic message required

The standard is not always as coherent as we would like, since it is a very large document, written (in practice) by a number of different people, and despite all of the proof-reading that does occur, inconsistencies slip through. In the case of undefined behavior (and errors in general), I think there is an additional … 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

Why is it undefined behavior to delete[] an array of derived objects via a base pointer?

Base* p = new Base[n] creates an n-sized array of Base elements, of which p then points to the first element. Base* p = new Derived[n] however, creates an n-sized array of Derived elements. p then points to the Base subobject of the first element. p does not however refer to the first element of … Read more

With C++11, is it undefined behavior to write f(x++), g(x++)?

No, the behavior is defined. To quote C++11 (n3337) [expr.comma/1]: A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded-value expression (Clause [expr]). Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. … Read more