Questions tagged [undefined-behavior]

The unpredictable outcome of compiling or executing a program which breaks rules of the language neither compiler, interpreter nor runtime-system have to enforce. DO NOT USE this tag for questions regarding the data type or return value of "undefined". In those cases, the [undefined] tag should be used instead.

In computer programming, undefined behavior (informally "UB") refers to computer code whose behavior is not specified by the programming language standard under certain conditions.

The standards for some languages, most notably C and C++, leave certain aspects undefined, meaning the standard imposes no requirements whatsoever on the outcome. Implementations may regard such actions as erroneous, diagnosing them or not as they see fit, or may specify that they behave in some possibly-useful fashion without regard for whether the Standard requires them to do so.

For example, accessing beyond the last element of an array in C might be diagnosed by the compiler if the array index is known during compilation, or might return a garbage value from uninitialized memory, or return an apparently sensible value, or cause the program to crash by accessing memory outside the process' data address space.

Undefined Behavior most often causes confusion and controversy in situations where some parts of the Standard, an implementation's documentation, an earlier standard, or K&R's The C Programming Language describe the behavior of some action, but another part of the Standard characterizes the action as Undefined Behavior. In cases where the described behavior happens to be useful for the task at hand, the cost of behaving as described would often be less than the cost to the programmer of having to find some other way to accomplish the same task. In cases where it does not benefit the task at hand, however, upholding the behavior may add cost with no offsetting benefit. Because implementations are used for a variety of tasks with differing requirements, and because compiler writers should be better placed than the committee to know their various customers' needs, the authors of the Standard treat the question of whether to behave as described in such cases as a "quality of implementation" issue outside their jurisdiction.

Undefined behavior is commonly shortened to "UB". This informal abbreviation appears frequently in discussions regarding undefined behavior.

Formal definition of undefined behavior, as stated by the standards:

C11 3.4.3

undefined behavior
behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

C++11 1.3.24

undefined behavior
behavior for which this International Standard imposes no requirements

[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]

Tag usage

Use this tag for:

  • Questions asking if a certain code contains undefined behavior.
  • Questions regarding the various forms of documented undefined behavior in the given programming language.

Avoid using this tag for:

  • Questions asking why undefined behavior gave a certain result. It is often impossible to provide a meaningful answer to such questions.

Always use this tag together with the relevant programming language tag.

Useful C and C++ questions / canonical duplicates

How to explain undefined behavior to know-it-all newbies?
Undefined, unspecified and implementation-defined behavior.
Undefined behavior and sequence points.
Why are these constructs (using ++) undefined behavior?

External links

Wikipedia: Undefined behaviour
A Guide to Undefined Behavior in C and C++, Part 1
What Every C Programmer Should Know About Undefined Behavior, part 2, part 3
Isn't a Standard's whole job to standardize these things?
Implementation-defined, unspecified, and undefined behavior. What do these mean?
I just tried it on an ANSI-conforming compiler, and got the results I expected

2152 questions
1023
votes
5 answers

What are "sequence points" and how do they affect undefined behavior?

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 should I stop using them? If you've read this, be sure…
Prasoon Saurav
  • 85,400
  • 43
  • 231
  • 337
861
votes
11 answers

What is the strict aliasing rule?

When asking about common undefined behavior in C, people sometimes refer to the strict aliasing rule. What are they talking about?
Benoit
  • 34,830
  • 24
  • 79
  • 113
854
votes
14 answers

Why are these constructs using pre and post-increment undefined behavior?

#include 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 = u++ + ++u; printf("%d\n", u); // 1 u = 1; …
PiX
  • 9,147
  • 4
  • 17
  • 11
574
votes
8 answers

Undefined, unspecified and implementation-defined behavior

What is undefined behavior in C and C++? What about unspecified behavior and implementation-defined behavior? What is the difference between them?
524
votes
5 answers

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

I know that an "undefined behaviour" in C++ can pretty much allow the compiler to do anything it wants. However, I had a crash that surprised me, as I assumed that the code was safe enough. In this case, the real problem happened only on a specific…
Remz
  • 2,899
  • 3
  • 5
  • 10
331
votes
22 answers

Is uninitialized local variable the fastest random number generator?

I know the uninitialized local variable is undefined behaviour(UB), and also the value may have trap representations which may affect further operation, but sometimes I want to use the random number only for visual representation and will not…
ggrr
  • 7,361
  • 5
  • 22
  • 44
274
votes
11 answers

Why is f(i = -1, i = -1) undefined behavior?

I was reading about order of evaluation violations, and they give an example that puzzles me. 1) If a side effect on a scalar object is un-sequenced relative to another side effect on the same scalar object, the behavior is undefined. // snip f(i…
Nicu Stiurca
  • 8,024
  • 5
  • 37
  • 46
242
votes
14 answers

Why does this for loop exit on some platforms and not on others?

I have recently started to learn C and I am taking a class with C as the subject. I'm currently playing around with loops and I'm running into some odd behaviour which I don't know how to explain. #include int main() { int…
JonCav
  • 1,657
  • 2
  • 8
  • 8
227
votes
5 answers

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

Unsigned integer overflow is well defined by both the C and C++ standards. For example, the C99 standard (§6.2.5/9) states A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting…
anthonyvd
  • 6,667
  • 4
  • 28
  • 48
201
votes
11 answers

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++;
yesraaj
  • 42,284
  • 65
  • 185
  • 246
164
votes
15 answers

In practice, why would different compilers compute different values of int x = ++i + ++i;?

Consider this code: int i = 1; int x = ++i + ++i; We have some guesses for what a compiler might do for this code, assuming it compiles. both ++i return 2, resulting in x=4. one ++i returns 2 and the other returns 3, resulting in x=5. both ++i…
cinnamon
  • 1,732
  • 2
  • 4
  • 11
163
votes
5 answers

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

Compiling this: #include int main() { for (int i = 0; i < 4; ++i) std::cout << i*1000000000 << std::endl; } and gcc produces the following warning: warning: iteration 3u invokes undefined behavior…
zerkms
  • 230,357
  • 57
  • 408
  • 498
155
votes
8 answers

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

Updated, see below! I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet #include int main() { while(1) ; std::cout << "Hello" << std::endl; } It apparently has something to do with…
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175
148
votes
5 answers

Why does the enhanced GCC 6 optimizer break practical C++ code?

GCC 6 has a new optimizer feature: It assumes that this is always not null and optimizes based on that. Value range propagation now assumes that the this pointer of C++ member functions is non-null. This eliminates common null pointer checks but…
boot4life
  • 4,292
  • 5
  • 20
  • 40
133
votes
5 answers

Accessing inactive union member and undefined behavior?

I was under the impression that accessing a 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 from the standard). So, is it undefined behavior?
Luchian Grigore
  • 236,802
  • 53
  • 428
  • 594
1
2 3
99 100