One-dimensional access to a multidimensional array: is it well-defined behaviour?

All arrays (including multidimensional ones) are padding-free. Even if it’s never explicitly mentioned, it can be inferred from sizeof rules. Now, array subscription is a special case of pointer arithmetics, and C99 section 6.5.6, §8 states clearly that behaviour is only defined if the pointer operand and the resulting pointer lie in the same array … Read more

reinterpret_cast creating a trivially default-constructible object

There is no X object, living or otherwise, so pretending that there is one results in undefined behavior. [intro.object]/1 spells out exhaustively when objects are created: An object is created by a definition ([basic.def]), by a new-expression ([expr.new]), when implicitly changing the active member of a union ([class.union]), or when a temporary object is created … Read more

Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?

The paragraph you copied is talking about unsigned types. The behavior is undefined in C++. From the last C++0x draft: The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more … Read more

Do I need to cast to unsigned char before calling toupper(), tolower(), et al.?

Yes, the argument to toupper needs to be converted to unsigned char to avoid the risk of undefined behavior. The types char, signed char, and unsigned char are three distinct types. char has the same range and representation as either signed char or unsigned char. (Plain char is very commonly signed and able to represent … Read more