Optimizing away a “while(1);” in C++0x

To me, the relevant justification is: This is intended to allow compiler transfor- mations, such as removal of empty loops, even when termination cannot be proven. Presumably, this is because proving termination mechanically is difficult, and the inability to prove termination hampers compilers which could otherwise make useful transformations, such as moving nondependent operations from … Read more

Why does this loop produce “warning: iteration 3u invokes undefined behavior” and output more than 4 lines?

Signed integer overflow (as strictly speaking, there is no such thing as “unsigned integer overflow”) means undefined behaviour. And this means anything can happen, and discussing why does it happen under the rules of C++ doesn’t make sense. C++11 draft N3337: §5.4:1 If during the evaluation of an expression, the result is not mathematically defined … Read more

What are all the common undefined behaviours that a C++ programmer should know about? [closed]

Pointer Dereferencing a NULL pointer Dereferencing a pointer returned by a “new” allocation of size zero Using pointers to objects whose lifetime has ended (for instance, stack allocated objects or deleted objects) Dereferencing a pointer that has not yet been definitely initialized Performing pointer arithmetic that yields a result outside the boundaries (either above or … Read more

Why is unsigned integer overflow defined behavior but signed integer overflow isn’t?

The historical reason is that most C implementations (compilers) just used whatever overflow behaviour was easiest to implement with the integer representation it used. C implementations usually used the same representation used by the CPU – so the overflow behavior followed from the integer representation used by the CPU. In practice, it is only the … Read more

Is uninitialized local variable the fastest random number generator?

As others have noted, this is Undefined Behavior (UB). In practice, it will (probably) actually (kind of) work. Reading from an uninitialized register on x86[-64] architectures will indeed produce garbage results, and probably won’t do anything bad (as opposed to e.g. Itanium, where registers can be flagged as invalid, so that reads propagate errors like … Read more

Does the C++ standard allow for an uninitialized bool to crash a program?

Yes, ISO C++ allows (but doesn’t require) implementations to make this choice. But also note that ISO C++ allows a compiler to emit code that crashes on purpose (e.g. with an illegal instruction) if the program encounters UB, e.g. as a way to help you find errors. (Or because it’s a DeathStation 9000. Being strictly … Read more

Undefined, unspecified and implementation-defined behavior

Undefined behavior is one of those aspects of the C and C++ language that can be surprising to programmers coming from other languages (other languages try to hide it better). Basically, it is possible to write C++ programs that do not behave in a predictable way, even though many C++ compilers will not report any … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)