Normally when a foo() have memory problem the whole program will simply core dump, crash. But does C have any protection ways to guarantee a program will not crash, just goto a state or simply return, when core dump happens inside foo()?
Asked
Active
Viewed 55 times
-1
-
2If there's a memory problem, you **want** it to crash. Otherwise, your program will likely exhibit strange behavior you can't explain. – dbush Dec 05 '16 at 15:44
-
To enlarge on the @dbush comment, unfortunately there is no guarantee that a "memory problem" will actually crash a C program - it can just give wrong results. All you can say is that a crash *might eventually* occur. Results can also be non-deterministic, depending on the cause, particularly in multi-threaded programs. – cdarke Dec 05 '16 at 15:56
-
By the way, there is no `try` and `catch` in standard C. Windows C supports `__try` and `__except`, but that is non-standard. – cdarke Dec 05 '16 at 15:58
-
Thanks for your reply guys. I mean, is there anyway when a function has memory problem, do not let it core dump but stop and show some message. I google out some function inside setjmp.h may help. Are there any other solutions? – 彭浩翔 Dec 05 '16 at 16:07
-
I dont think so, function know that it has core dump only when it happens. There is no way how to predict it (for the function). – Dec 05 '16 at 16:11
-
The only way to get round a crash is to avoid situations causing it. – alk Dec 05 '16 at 18:06
-
@alk This is useless as wear more cloth to avoid sick – 彭浩翔 Feb 16 '17 at 06:17
1 Answers
0
Can not say for try and catch statement in c but yes you can write signal handler to catch segfault. Here is a link. How to catch segmentation fault in Linux?

Community
- 1
- 1

Sumit Gemini
- 1,836
- 1
- 15
- 19
-
http://stackoverflow.com/questions/2350489/how-to-catch-segmentation-fault-in-linux/2436368#2436368 – KevinDTimm Dec 05 '16 at 17:40
-
Catching a `SIGSEGV` mostly never helps. If the stack had been smashed, or memory management info got deleted, messed-up, or WTF got lost there is no chance to recover from this. – alk Dec 05 '16 at 18:05