gpt4 book ai didi

C++/海湾合作委员会 : segmentation fault at exit of main after catching a signal

转载 作者:行者123 更新时间:2023-11-30 03:10:08 26 4
gpt4 key购买 nike

以下代码在退出时触发段错误。它似乎只有在“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com