Nik*_*hil 3 c linux malloc undefined-behavior allocator
请告诉我堆栈和堆之间的区别与下面的代码
int main()
{
int arr[3];
int *a;
arr [5] = 6; // out of bound but it will not give error.
arr [3000] = 8 ; //SIGSEGV
a = malloc (sizeof (int));
a[4] = 6;
a[4000] = 8; //No error
}
Run Code Online (Sandbox Code Playgroud)
我知道arr是一个静态数组,当我执行arr [3000]时会访问其他进程的地址,这会给出SIGSEGV错误.但我不明白为什么[4000]不会给我任何运行时错误,即SIGSEGV信号.
谢谢
不能保证这些调用中的任何一个实际写入无法访问的内存(这会触发段错误).程序有权写入该内存并且不会触发段错误,但会覆盖与您的阵列无关的其他一些内部数据.这当然可能会导致程序中其他地方的意外影响.
通常,这称为未定义行为.当你写出数组的边界时,没有任何承诺会发生什么,任何可能发生或可能不发生的事情.
| 归档时间: |
|
| 查看次数: |
1034 次 |
| 最近记录: |