[Solved] Can code that doesn’t execute cause segmentation fault? [closed]


No, code which doesn’t execute can’t cause a segmentation fault.

Some other code (executed before the code you have shown) will be the cause.

The other code is exhibiting undefined behaviour (as per the meaning of “undefined” in the C++ standard), and overwriting some area of memory it shouldn’t. Removing the else block from your code can cause an implementation (compiler, linker, etc) to restructure how your program and data it uses is laid out in memory (e.g. where code and data are located in memory). That can, in turn, change the effects of your offending code overwriting memory (e.g. it changes whether the overwritten memory is being used to store a variable, or it is some area of memory that your operating system deems your program should not access).

0

solved Can code that doesn’t execute cause segmentation fault? [closed]