First of all, you aren’t guarenteed to get a segmentation fault, or any defined behavior, for triggering undefined behavior, such as modifying a string literal. Though on Linux, string literals are put into read-only memory, so modifying them on Linux will usually result in a segmentation fault.
So why doesn’t this code trigger a segfault? It does, but you just don’t see it, because you forked.
The “Segmentation Fault (core dumped)” message is printed by your shell (ex. bash), when it detects, by calling wait
, when the process it spawned terminates with a SIGSEGV
(segfault) signal. However, your shell will not receive any notifications about child processes exiting; your parent process will.
Your parent process calls wait
to wait and get the exit code when it terminates. If you weren’t ignoring the exit code by passing in NULL
to wait, you’d probably see that the exit code is -11
, or -SIGSEGV
.
So it is segfaulting; your parent process is just ignoring the child’s notification that it segfaulted.
4
solved Not Getting Segmentation Fault [duplicate]