Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
Undefined behavior

Undefined behavior

May 23 2015

Undefined behavior is a characteristic of certain programming languages (most prominent in C and C++) to produce a result in certain situations that depends on compiler implementation or specified optimization switches. In other words, the specification does not define the language's behavior in all possible situations but states: "in case of the A condition, the result of B operation is undefined". It is considered a mistake to allow such a situation in your program even if it is executed correctly with some particular compiler. Such a program won't be a crossplatform one and may cause failures on a different computer, operating system and even with different compiler's settings.

You should not mix up undefined behavior with unspecified behavior, the latter being the case when the specification does allow not every behavior possible but a restricted range of implementation variants.

Below are examples of situations causing undefined behavior:

  • Using a variable before initializing it. Undefined behavior occurs when trying to use the variable.
  • Memory allocation using the new [] operator and subsequent release using the delete operator. For example: T *p = new T[10]; delete p;. The correct code is: T *p = new T[10]; delete [] p;.
  • A variable is changed several times within one sequence point. As a canonical example, the i=i++ expression is often cited where assignment of the i variable and its increment are performed at the same time. To learn more about this kind of errors, read the section "sequence points".

References

Popular related articles


Comments (0)

Next comments next comments
close comment form
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam