Skip to content

UB46⚓︎

author: Xiangzhi Liu.

Definition⚓︎

An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).

Description⚓︎

数组下标越界(即便访问到了正确的内存)的行为是未定义的。

Code⚓︎

UB46.c
#include <stdio.h>

int main() {
    int a[2][3] = {{1,2,3},{4,5,6}};
    printf("%d\n", a[0][3]);
    return 0;
}

Configurations⚓︎

gcc version 4.9.2

Target: x86_64-w64-mingw32

MSVC _MSC_VER = 1928

Target: x86_64

Behaviors⚓︎

编译通过无警告 输出:4

编译通过无警告 输出:4

Advice⚓︎

程序员应保证不出现任何数组越界的行为。