作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码在退出时触发段错误。它似乎只有在“sigaction”调用和循环之间的堆栈上分配数据时才会发生:
#include <signal.h>
#include <unistd.h>
bool end = false;
void handler(int) {
end = true;
}
int main() {
struct sigaction sigs;
sigs.sa_handler = handler;
sigaction(SIGINT, &sigs, NULL);
int i;
while (!end)
sleep(1);
return 0;
}
使用 ctrl-C 运行和停止
-> 行 'int i' on : segmentation fault
-> 没有行 'int i' : 退出 ok
(使用 g++ v4.1.1 编译,操作系统 linux 内核 2.6.19)
听起来像是堆栈释放问题......有人有解释吗?
谢谢,
最佳答案
您应该初始化 struct sigaction 的所有成员,以免它包含垃圾,有标志/等等。在那里改变了 sigaction() 的行为做
struct sigaction args = {};
或
memset(&args,0,sizeof args);
关于C++/海湾合作委员会 : segmentation fault at exit of main after catching a signal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3460559/
我是一名优秀的程序员,十分优秀!