storage size of ‘oldact’ isn’t known

#include <signal.h>

int main()
{
struct sigaction act, oldact;
return 0;
}


dies with the message


testgcc4.c: In function ‘main’:
testgcc4.c:6: error: storage size of ‘act’ isn’t known
testgcc4.c:6: error: storage size of ‘oldact’ isn’t known


if I use -std=c99 or --ansi?
gcc testgcc4.c compiles without problem but adding -std=c99 or --ansi produces
the error message above (for any of gcc-3.4, gcc-4.0, gcc-4.1) but neither

icc nor gcc on FreeBSD has a problem with -std=c99 or --ansi


Sumarry:

use -std=gnu99 on linux if you need some C99 functions (e.g. strtof) but also want to use functions like sigaction and strtok_r

gcc on linux is stricter than gcc on FreeBSD (or icc on either FreeBSD or linux) concerning standards.
gcc -std=c99 on linux means: *only* recognize functions specified in the standard (no sigaction, no strtok_r etc)
gcc -std=c99 on FreeBSD or icc -std=c99 on either FreeBSD or linux means recognize functions specified in the standard. This corresponds more or less to -std=gnu99 on linux

PS. This trap seems to be quite common (e.g. discussions on debian-glibc about the "fact" that strtof is completely broken on linux). I would be grateful if somebody could point me to a good overview describing what standards compliance means for various OS/compiler+options/standards combinations.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
sigaction是一个POSIX标准的系统调用函数,用于捕捉和处理信号。它的原型如下: ```c int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); ``` 参数说明: - signum:需要捕捉的信号编号。 - act:指向新的信号处理函数的结构体指针。 - oldact:指向旧的信号处理函数的结构体指针。 sigaction所使用的结构体是struct sigaction,其定义如下: ```c struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); }; ``` 其中,各字段的含义如下: - sa_handler:指向信号处理函数的指针,如果sa_flags字段的值为SA_SIGINFO,则该字段被忽略。 - sa_sigaction:指向信号处理函数的指针,如果sa_flags字段的值不为SA_SIGINFO,则该字段被忽略。 - sa_mask:指定了在信号处理函数执行期间需要屏蔽的信号集。 - sa_flags:指定了信号处理的行为。常见的取值包括: - SA_RESTART:如果在信号处理函数执行期间被信号中断,则系统自动重启被中断的系统调用。 - SA_NODEFER:在信号处理函数执行期间不会屏蔽当前信号。 - SA_SIGINFO:指定sa_sigaction字段将被用作信号处理函数。 - sa_restorer:指定了一个指针,该指针指向一个用于恢复系统调用错误状态的函数。 sigaction函数的返回值为0表示成功,否则表示出现了错误。如果oldact参数不为空,则系统将旧的信号处理函数信息保存到该结构体中。 sigaction函数的使用方法: ```c void handle_signal(int signum) { // 信号处理函数 } int main() { struct sigaction act, oldact; // 注册信号处理函数 act.sa_handler = handle_signal; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, &oldact); // 循环等待信号 while (1) { // do something } // 恢复旧的信号处理函数 sigaction(SIGINT, &oldact, NULL); return 0; } ``` 以上代码注册了一个SIGINT信号处理函数,并在程序结束前恢复了旧的信号处理函数。在信号处理函数中,可以使用其他系统调用函数来完成一些特定的任务,例如使用write函数向标准输出打印一些信息。但是需要注意的是,在信号处理函数中不能使用非可重入函数,因为在信号处理函数执行期间可能会再次收到相同的信号,导致函数调用重入。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值