Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Undefined Behavior

C Undefined Behavior

In C, **"undefined behavior"** refers to program behavior that is not explicitly defined by the C language standard and can therefore manifest as any outcome.

This means that when a program exhibits undefined behavior, it may produce unpredictable results, including program crashes, data corruption, security vulnerabilities, and it may even appear to run normally.

Undefined behavior is an important concept in C because it concerns the correctness and safety of a program.

Here are some common situations that can lead to undefined behavior:

Array out of bounds

When we try to access an out-of-bounds element of an array, that is, an element before the 0th element or after the array's length, the compiler cannot determine what is stored in the memory location being accessed, which leads to undefined behavior. For example:



int arr[3] = {1, 2, 3};

printf("%d\n", arr[5]); // 越界访问,结果未定义

Dereferencing a null pointer

When we try to dereference a null pointer, the compiler cannot determine what is stored in the memory location to be accessed, which leads to undefined behavior. For example:

int *ptr = NULL;

printf("%d\n", *ptr); // 解引用空指针,结果未定义

Uninitialized local variables

When we use an uninitialized local variable, its value is undefined, which leads to undefined behavior. For example:



int x;

printf("%d\n", x); // x 未初始化,结果未定义

Floating-point division by zero

When we try to divide a floating-point number by zero, the result is undefined. For example:



float x = 1.0;

float y = x / 0.0; // 浮点数除以零,结果未定义

Integer division by zero

When we try to divide an integer by zero, the result is undefined. For example:



int x = 10;

int y = x / 0; // 整数除以零,结果未定义

Signed overflow

When an integer operation produces a result that exceeds the range representable by the integer type, the result is undefined. For example:



signed char x = 127;

x = x + 1; // signed char 溢出,结果未定义

Shift count too large

When performing a shift operation, if the number of bits shifted is greater than or equal to the width of the operand, the result is undefined. For example:



int x = 1;

int y = x << 32; // 位移操作数太大,结果未定义

Incorrect type conversion

When we perform an unsafe type conversion, the result is undefined. For example:



int *ptr = (int *)malloc(sizeof(int));

float *fptr = (float *)ptr; // 错误的类型转换,结果未定义

Memory out of bounds

When we write to memory that has already been freed or was never allocated, the result is undefined. For example:



int *ptr = (int *)malloc(sizeof(int));

free(ptr);

*ptr = 10; // Out of bounds memory, undefined result

Undefined floating-point behavior

For example, comparing two NaN (Not a Number) values for equality is undefined behavior. For example:

float x = sqrt(-1);

float y = sqrt(-1);

if (x == y) {

    printf("NaN values are equal\n");

}

Others

There are some other undefined behaviors:

These are all situations to avoid during programming, because they can cause a program to behave unpredictably in different environments, making the code non-portable and potentially leading to errors.

How to avoid it

To avoid undefined behavior, during development we need to:

Undefined behavior is a complex and dangerous concept in C. It requires developers to have a deep understanding of the language's rules and to take appropriate measures to avoid it. By following best practices and using the right tools, you can minimize the risk of undefined behavior, thereby improving the reliability and safety of your program.