Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation fault before entering main [closed]

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.

like image 586
moosefoot Avatar asked Feb 25 '17 05:02

moosefoot


People also ask

How do you fix a segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

What triggers segmentation fault?

A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

Does segmentation fault mean memory leak?

Most memory errors which aren't memory leaks end up resulting in a segmentation fault. A segmentation fault is raised when the operating system realizes that your program is trying to access memory that it shouldn't have access to.


Video Answer


2 Answers

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

like image 129
Squirrel Avatar answered Sep 30 '22 08:09

Squirrel


Found the error. I had a struct with an array of 50 other structs in it, with every element in that array having a an array of 50 other structs as well. I quickly changed the size of the arrays to 1 to make sure this was the cause, and so it was.

Code in main now executes.

like image 44
moosefoot Avatar answered Sep 30 '22 10:09

moosefoot