The following code can be well compiled, however when execute it, GCC crashes with segmentation fault. ======================== #include <iostream> #include <exception> int main(){ try{ throw -1; } catch(int e){ main(); } return 0; } ======================== Segmentation fault (core dumped) gcc version 9.3.0 on Ubuntu 20.04
Well, the code causes a stack overflow, it's invalid if I see correctly.
g++ pr103153.C && valgrind ./a.out ==1687== Memcheck, a memory error detector ==1687== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==1687== Using Valgrind-3.17.0.GIT and LibVEX; rerun with -h for copyright info ==1687== Command: ./a.out ==1687== ==1687== Stack overflow in thread #1: can't grow stack to 0x1ffe801000 ==1687== ==1687== Process terminating with default action of signal 11 (SIGSEGV): dumping core ==1687== Access not within mapped region at address 0x1FFE801FE8 ==1687== Stack overflow in thread #1: can't grow stack to 0x1ffe801000 ==1687== at 0x4B9D520: get_cie_encoding (unwind-dw2-fde.c:300) ==1687== If you believe this happened as a result of a stack ==1687== overflow in your program's main thread (unlikely but ==1687== possible), you can try to increase the size of the ==1687== main thread stack using the --main-stacksize= flag. ==1687== The main thread stack size used in this run was 8388608. ==1687== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
"however when execute it" Yes because the code has an infinite stack usage.
The code is invalid both because of the endless recursion into which it runs and because in C++ main can't be called. See https://eel.is/c++draft/basic.start.main#3
(In reply to Xinmeng Xia from comment #0) > The following code can be well compiled, however when execute it, GCC > crashes with segmentation fault. No, your program crashes, GCC doesn't. Any use of main is undefined in C++, especially calling it. This program is just broken and of course it crashes.