Array index out of bound in C

后端 未结 10 1128
抹茶落季
抹茶落季 2020-11-21 21:58

Why does C differentiates in case of array index out of bound

#include 
int main()
{
    int a[10];
    a[3]=4;
    a[11]=3;//doe         


        
10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 22:48

    You generally only get a segmentation fault if you try to access memory your process doesn't own.

    What you're seeing in the case of a[11] (and a[10] by the way) is memory that your process does own but doesn't belong to the a[] array. a[25000] is so far from a[], it's probably outside your memory altogether.

    Changing a[11] is far more insidious as it silently affects a different variable (or the stack frame which may cause a different segmentation fault when your function returns).

提交回复
热议问题