-2
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int *ptr,i;
ptr=(int*)malloc(sizeof(int));
printf("sizo of ptr is:%d",sizeof(ptr));
for(i=0;i<30;i++)
scanf("%d",ptr+i);
for(i=0;i<30;i++)
printf("%d",*(ptr+i));
getch();
}
这里ptr的大小是:4 我的问题是,我想在ptr中只存储一个整数,但在这个程序中我可以存储超过30或100等,为什么它不会抛出错误?malloc真正分配多少内存?
因为今天你有一些运气。它可能会在明天崩溃。 – jweyrich 2014-09-04 13:31:29
我在linux和windows中检查过这个程序。两者都给出了相同的结果。目前我正在使用dev C++。 – user3614789 2014-09-04 13:34:38
相关的问题,关于溢出堆栈上的缓冲区(相同的一般答案):http://stackoverflow.com/questions/23226217/why-buffer-overflow-doesnt-affect-to-this-code – 2014-09-04 13:45:38