Namespaces
Variants
Actions

Glossary

From cppreference.com
Revision as of 23:54, 4 November 2013 by Indi (Talk | contribs)


This page describes several concepts that will be frequently used in subsequent pages. You don't need to learn all of them just yet -- leave this for when you encounter them in other articles.

Compiler

A program that takes textual representation of code and produces a program file that contains machine instructions for a particular processor.

Undefined behavior

Undefined behavior happens when you break certain rules of C++ in your code. It means that the compiler is allowed to do anything with your code. For example, the compiler may:

  • refuse to compile code and return an error message instead.
  • compile the code successfully, but the program crashes when run.
  • compile the code successfully, but the program produces wrong results when run.
  • compile the code successfully, but the program works on one computer, but crashes on another.
  • etc.

In short, the results of the compilation are unpredictable. A program may appear to run well, but as far as C++ compiler is concerned, the program is broken and even small changes to the environment it runs in may lead to a program crash or to the program returning incorrect results. Thus undefined behavior should be avoided at all costs.

Undefined behavior is not a silly idea as it might look like. Two of the primary goals of C++ are performance and portability. These aims are contradictory: given the variety of microprocessors in the world, it is almost insurmountable task to design a language that allows code to run well on all of them. C++ solves this issue by simply disallowing certain behaviors of the program. Since the compiler may not be able to detect whether those rules are violated, the C++ language allows it to produce more or less broken programs. Therefore, the programmer must be very careful.

See also extended description and condition list.

As if rule

The “as-if” rule allows flexibility in C++ implementations. The “as-if” rule is: “Implementations are free to reorder or even rewrite a program’s instructions, so long as the resulting program still behaves as if it were exactly as written.

For example, consider the following program:

#include <iostream>
 
int main()
{
    int a = 10;
    int b = 20;
 
    std::cout << "a = " << a << '\n';
    std::cout << "b = " << b << '\n';
 
    return 0;
}

Output:

a = 10
b = 20

The “as-if” rule means that the lines setting a to 10 and b to 20 don’t necessarily need to be run in that order. b may be set first, then a. In fact, b may not be set until after the line that prints the value of a! All that has to happen is that a must be set to 10 before the line that prints the value of a, b must be set to 20 before its value is printed (because in both cases, the value cannot be printed before it is set), and the two print statements cannot be swapped (otherwise the program’s output would be different). Beyond those restrictions, it doesn’t matter in what order things happen. You can shuffle the instructions around, and the program’s output will still be the same.

This is a very important factor in C++’s efficiency. Take another look at the example above. If you think about it, it’s even possible to completely eliminate both variables – they never change, after all, and the values are used just a few lines later. The code could be:

#include <iostream>
 
int main()
{
    std::cout << "a = " << 10 << '\n';
    std::cout << "b = " << 20 << '\n';
 
    return 0;
}

Output:

a = 10
b = 20

and it would produce exactly the same observable output, while saving the memory and extra instructions associated with the two variables.

The “as-if” rule allows you to write what you mean in the clearest, most logical way, while giving the compiler the freedom to rearrange things, or even eliminate things, to produce the most efficient program.

One definition rule