Skip to content
Related Articles

Related Articles

Improve Article

Undefined Behavior in C and C++

  • Difficulty Level : Easy

Consider the following C/C++ programs and try to guess the output? 
 

C




// Program 1 (Divide by 0)
int main()
{
    int x = 25, y = 0;
    int z = x / y;
    printf("Hello World!");
    return 0;
}

C




// Program 2 (Uninitialized variables)
int main()
{
    bool val;
    if (val)
        printf("TRUE");
    else
        printf("FALSE");
}

C




// Program 3 (Accessing value of NULL
// pointer)
int main()
{
   int *ptr = NULL;
   printf("%d", *ptr);
   return 0;
}

C




// Program 4 (Accessing out of bound)
int main()
{
   int arr[5];
 
   // We access arr[5] in last iteration.
   for (int i=0; i<=5; i++)
      printf("%d ", arr[i]);
}

C




// Program 5 (Going beyond limit of
// signed int)
int main()
{
   int x = INT_MAX;
   printf("%d", x+1);
   return 0;
}

C




// Program 6 (Trying to modify a string
// literal)
int main()
{
   char *s = "geeksforgeeks";
   s[0] = 'e';
   return 0;
}

C




// Program 7 (Modifying a variable
// multiple times before a defined
// sequence point)
#include <stdio.h>
int main()
{
   int i = 8;
   int p = i++*i++;
   printf("%d\n", p);
}

The output of all of the above programs is unpredictable (or undefined). The compilers (implementing the C/C++ standard) are free to do anything as these are undefined by the C and C++ standards. 
Language like Java traps errors as soon as they are found but languages like C and C++ in few cases keep on executing the code in a silent but faulty manner which may result in unpredictable results. The program can crash with any type of error message, or it can unknowingly corrupt the data which is a grave issue to deal with. 
Importance of knowing about Undefined Behavior 
If a user starts learning in a C/C++ environment and is unclear with the concept of undefined behavior then that can bring plenty of problems in the future like while debugging someone else code might be actually difficult in tracing the root to the undefined error.
Risks and Disadvantages of Undefined Behavior 
The programmers sometimes rely on a particular implementation (or compiler) of undefined behavior which may cause problems when the compiler is changed/upgraded. For example, the last program produces 72 as output in most of the compilers, but implementing software based on this assumption is not a good idea. 
Undefined behaviors may also cause security vulnerabilities, especially due to the cases when an array out of bound is not checked (causes buffer overflow attack).
Advantages of Undefined Behavior 
C and C++ have undefined behaviors because it allows compilers to avoid lots of checks. Suppose a set of code with a greater performing array need not keep a look at the bounds, which avoids the need for a complex optimization pass to check such conditions outside loops. The tightly bound loops and speed up the program from thirty to fifty percent when it gains an advantage of the undefined nature of signed overflow, which is generally offered by the C compiler. 
We also have another advantage of this as it allows us to store a variable’s value in a processor register and manipulate it over time that is larger than the variable in the source code. It also helps in wrap-around then compile-time checks which would not be possible without the greater knowledge of the undefined behavior in the C/C++ compiler.
More examples of undefined behavior 
 

  1. Sequence Points in C | Set 1
  2. “delete this” in C++
  3. Passing NULL to printf in C
  4. Accessing array out of bounds in C/C++
  5. Use of realloc()
  6. Execution of printf with ++ operatorsVirtual destruction using shared_ptr in C++
  7. Virtual Destructor

References:  

 

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :