背景导入
微信截图_20180428165124.png
是否有过这样的经历的,当你兴致满满的编译好你的C文件后,运行的时候却出现了这样子的惨痛经历。下面,我将总结几种出现这种问题的原因。
栈区过大
# include "stdio.h"
int main(){
int MB[209715200000];
printf("allocate the memory successfully!");
return 0;
}
当然,为了效果明显,我们故意将只分配弄得特别大,所以这里,我们运行一下可以看到:
$gcc -o main *.c
$main
timeout: the monitored command dumped core
sh: line 1: 72469 Segmentation fault timeout 10s main
错误地址访问
指针偏移
# include "stdio.h"
int main(){
int x;
scanf("%d", x);
printf("运行完成.");
}
结果也是你输入值之后就提示