- 论坛徽章:
- 0
|
cugb_cat兄,不好意思,刚才是我测试的问题。我刚才发现另外一个问题:如果malloc失败后继续使用,会导致段错误,但这时的段错误能用信号捕捉到;如果是栈上分配错误时后使用导致的段错误则捕获不到。- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- void signal_handler(int signo) {
- printf("get signal %d\n",signo);
- }
- void test(void) {
- unsigned int a[100000000];
- printf("%d\n",a[0]);
- }
- int main()
- {
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_handler = signal_handler;
- sigaction(SIGSEGV, &act, NULL);
- test();
- return 0;
- }
复制代码- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- void signal_handler(int signo) {
- printf("get signal %d\n",signo);
- }
- void test(void) {
- int *a = malloc(1000000000);
- printf("%d\n",a[0]);
- }
- int main()
- {
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_handler = signal_handler;
- sigaction(SIGSEGV, &act, NULL);
- test();
- return 0;
- }
复制代码 |
|