Search code examples
csegmentation-faultgdbargvargc

Segmentation fault before entering main


I recently made minor changes to previously-working code, and the program now immediately encounters a segmentation fault upon execution. In fact, it doesn't even make it to the first line in main.

Here is the beginning of the code:

int main (int argc, char* argv[])
{
    fprintf(stderr, "Not even getting here!\n");

    bool d;
    bool v;        
    ...
}

And the corresponding output from gdb (-g flag included upon compilation). warning: Error disabling address space randomization: Success

Program received signal SIGSEV, Segmentation fault.
0x0000000000400978 in main (argc=<error reading variable: Cannot access         
memory at address 0x7fffca168f1c, argv=<error reading variable: Cannot 
access memory at address 0x7fffca168f10>) at src/prog.c:35

FYI: Line 35 is just an opening brace ("{") for the main method. No actual code.

I have never encountered such an odd error before, I am baffled at how this happened. The code at the beginning was not altered at all before this error appeared, and the fact that the segmentation fault doesn't even occur anywhere near the new code is throwing me off greatly. Any code I put in main isn't being executed so I can't quite print out values to see what's going wrong.

Also, I have tried running the program with/without command-line args to see if that was the cause. It doesn't change anything.


Solution

  • Without the entire code it is difficult to say for sure, but based on other SO posts and personal experience I would suppose that you have too much space allocated to variables on the stack of main(). It would be useful for you to compare how many bytes are you using, and the stack size your program is allowed to have from the OS' perspective. See the following post: Segmentation Fault before main