Writing to pointer out of bounds after malloc() not causing error
Posted
by
marr
on Stack Overflow
See other posts from Stack Overflow
or by marr
Published on 2010-12-26T17:39:05Z
Indexed on
2010/12/26
17:54 UTC
Read the original article
Hit count: 205
c
|memory-corruption
Hi, when I try the code below it works fine. Am I missing something?
main()
{
int *p;
p=malloc(sizeof(int));
printf("size of p=%d\n",sizeof(p));
p[500]=999999;
printf("p[0]=%d",p[500]);
return 0;
}
I tried it with malloc(0*sizeof(int)) or anything but it works just fine. The program only crashes when I don't use malloc at all. So even if I allocate 0 memory for the array p, it still stores values properly. So why am I even bothering with malloc then?
© Stack Overflow or respective owner