I wrote this piece of code and was expecting a segmentation fault quicly, but it seems I am allowed to access pieces of memory I shouldn't be able to.
#include<stdio.h>
int main()
{
int tab[1];
tab[0]=42;
int i;
//Expecting Seg Fault from i==1...
for(i=0;;i++)
{
printf("%d %d
", i, tab[i]);
}
return 0;
}
I am compiling using:
gcc -Wall -Wextra my_code.c -o segfault && ./segfault
Upon execution, variable i
reaches values of order 1000 before I get my Segmentation Fault.
My question is: Why am I able to read tab
so far ?
PS: Using #include <stdlib.h>
and declaring int * tab = (int*)malloc(sizeof(int));
doesn't change anything...
Thanks, bests.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…