让这个 C 文件:
#include <stdio.h>
#include <signal.h>
void handle(){
return;
}
int main() {
struct sigaction action;
action.sa_handler = &handle;
printf("%d\n", action.sa_flags);
return 0;
}
使用 gcc-7 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 编译会引发警告:
src/flags.c:14:2: warning: ‘action.sa_flags’ is used uninitialized in this function [-Wuninitialized]
printf("%d\n", action.sa_flags);
但是,使用 gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.11),没有警告。
使用 gcc-7,该sa_flags
值实际上没有被初始化:
(gdb) x action.sa_flags
0x555545a0: Cannot access memory at address 0x555545a0
在 gcc-5 中,它得到一个奇怪的值:
(gdb) x action.sa_flags
0x4004a0 <_start>: 0x8949ed31
我不明白为什么:
- 使用 gcc-7,
sa_flags
不会初始化为 0。 - 使用 gcc-5,
sa_flags
并且_start
函数具有相同的地址。