All Questions

671
votes
13answers
54378 views

Why are these constructs (using ++) undefined behavior in C?

#include <stdio.h> int main(void) { int i = 0; i = i++ + ++i; printf("%d\n", i); // 3 i = 1; i = (i++); printf("%d\n", i); // 2 Should be 1, no ? volatile int u = 0; u...
864
votes
4answers
84798 views

Undefined behavior and sequence points

What are "sequence points"? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i;, to make myself feel better. Why shoul...
405
votes
8answers
36033 views

Undefined, unspecified and implementation-defined behavior

What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++?
621
votes
9answers
148093 views

What is the strict aliasing rule?

When asking about common undefined behavior in C, souls more enlightened than I referred to the strict aliasing rule. What are they talking about?
201
votes
11answers
49486 views

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

What are all the common undefined behaviours that a C++ programmer should know about? Say, like: a[i] = i++;
100
votes
2answers
12247 views

When does invoking a member function on a null instance result in undefined behavior?

Consider the following code: #include <iostream> struct foo { // (a): void bar() { std::cout << "gman was here" << std::endl; } // (b): void baz() { x = 5; } ...
5
votes
3answers
1166 views

Is segmentation fault actual undefined behavior when we refer to a non-static data-member

I had read the following rule and I've been trying to write an example, which reflects one. The rule is from 3.8/5 N3797: Before the lifetime of an object has started but after the storage which...
60
votes
7answers
7487 views

(Why) is using an uninitialized variable undefined behavior?

If I have: unsigned int x; x -= x; it's clear that x should be zero after this expression, but everywhere I look, they say the behavior of this code is undefined, not merely the value of x (until b...
83
votes
5answers
9282 views

Accessing inactive union member and undefined behavior?

I was under the impression that accessing an union member other than the last one set is UB, but I can't seem to find a solid reference (other than answers claiming it's UB but without any support fro...
21
votes
13answers
9422 views

C++ delete - It deletes my objects but I can still access the data?

I have written a simple, working tetris game with each block as an instance of a class singleblock. class SingleBlock { public: SingleBlock(int, int); ~SingleBlock(); int x; int ...

Previous Next