Namespaces
Variants
Actions

Difference between revisions of "glossary"

From cppreference.com
(+copy of two talk posts)
 
(+)
Line 1: Line 1:
 
{{title|Glossary}}
 
{{title|Glossary}}
  
This page describes several concepts that will be frequently used in subsequent pages.
+
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
  
The result of using forbidden constructs like indexing a vector outside it's bounds. Any program that uses them is ill-formed and will likely encounter unpredictable failures.
+
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.
 +
 
 +
* [[:enwiki:Undefined_behavior#Compiler_easter_eggs|launch a game]].
 +
 
 +
* [http://www.catb.org/jargon/html/N/nasal-demons.html try to make demons to fly out of your nose].
 +
 
 +
* 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.
  
Several constructs in C++ are forbidden by the standard. Using them may not be detected by the compiler and is likely too make your program fail in unpredictable ways. Even if it does not fail today with your current compiler-settings, it is very likely, that it will break in the future.
+
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.  
  
Note that not only the result of an illegal operation is undefined, but that the whole program is already broken when it starts and stays broken after it executed the illegal code. A real-world example have been integer-overflow-checks that were removed by compilers because at the point where the check would have been executed, the overflow would already have happened, making the check meaningless. (by FJW)
+
See also [[undefined behariov | extended description and condition list]].
  
            Stuff that will come up again and again even in introductory chapters. Like "undefined behaviour" (explaining that C++ is underspecified for efficiency and implementation flexibility, which means when you break the rules, anything could happen - it could even appear to work fine - but in "C++ speak" it basically means "bad"), the "as if rule" (explaining that a compiler is free to not do exactly what you wrote, so long as what it does behaves as if it were what you wrote), the "one definition rule" (explaining that "stuff" (functions, classes, etc.) can only be defined once in the program).
+
; As if rule
 +
{{todo}}
  
            You don't need to go into any detail - just set things up so that we can say stuff like "undefined behaviour" in topics and it won't be meaningless jargon (and so that we don't need to qualify everything we say - like we don't need to explain that creating a variable doesn't necessarily equal a variable actually being created, so long as the program behaves as if a variable was created). I don't know what to call it or I'd put it in the skeleton myself. Also, shouldn't we give a brief explanation of "compiling", "linking", etc. - ie, the build process?--Indi (talk) 22:17, 3 November 2013 (PST)
+
; One definition rule
 +
{{todo}}

Revision as of 03:16, 4 November 2013


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
One definition rule