C : 测试堆栈大小
出于教育目的,我尝试为堆栈超出大小生成分段错误。使用 ulimits -s 恢复堆栈大小(告诉我最大尺寸为 8192 kB)后,我尝试了以下最小代码:
int main()
{
char v[1024*8192];
return 0;
}
有效地,在 gcc file.c -O0 之后执行它/code> 它会引发分段错误。但是,它返回段。故障也具有较小的尺寸,例如,1024*8190
。我可以认为堆栈的一部分保留了执行所需的值。但是,奇怪的是,如果我将值指定为 1024*8184
或类似值并多次执行生成的程序,它会引发 seg.fault 有时是,有时不是。如果程序占用的堆栈相同,为什么有时会引发分段错误,有时则不会?
更新:根据要求,使用的代码正是我写的。
gcc版本为gcc版本9.4.0(Ubuntu 9.4.0-1ubuntu1~20.04)
使用的编译命令为gcc file.c -O0
For educational purposes, I trying to generate a segmentation fault for stack exeeding size. After recovering the stack size with ulimits -s
(telling me that the maximum dimensions is 8192 kB), I tried the following minimal code:
int main()
{
char v[1024*8192];
return 0;
}
Effectively, executing it after gcc file.c -O0
it raises a segmentation fault. However, it returns seg. fault also with a smaller size, e.g., 1024*8190
. I can think that a part of the stack is reserved with values needed for the execution. But, curiously, if I give value as 1024*8184
or similar and execute the resulting program several times, it raises seg.fault sometimes yes and sometimes no. If the stack occupation by the program is the same, why sometimes it raises segmentation fault and other times no?
UPDATE: as requested, the used code is exactly what I have written.
gcc version is gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)
the used compile command is gcc file.c -O0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论