Segmentation fault before entering main [closed]
Asked Answered
T

2

5

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.

Toritorie answered 25/2, 2017 at 5:28 Comment(10)
What is line 35 in src/prog.c? What changes did you make? If you revert those changes does it work then?Gothurd
Line 35 is "{". The opening bracket for main. As for the changes, they were made a few hours ago and the text editor was closed so I can not undo the changes made.Toritorie
Basics: does code #include <stdio.h>, #include <stdbool.h>?Whimsy
Even for small one-man projects, using a version control system like Git or Subversion is really useful. Then you could easily revert, "undo", changes without relying on the editors undo functionality. Other than that, was it a big change you made? Don't you remember what you did and can change it back manually?Gothurd
Code includes both of those. In fact, the code includes <assert.h>, <errno.h>, <limits.h>, <stdlib.h>, <string.h> and <time.h>. It also includes a header file I've made.Toritorie
My program parses a file using structs. The structs contain an array of other structs inside, and that struct also contains an array of struct as well. Struct 1 contains an array of 50 members of struct 2, which also contains 50 members of struct 3.Toritorie
Can you provide a code which can reproduce this error and share with us?Veraveracious
Possible duplicate of Segmentation Fault before mainMagruder
How much data is your program placing on the stack? In Windows, the stack limit (by default) is only 1 meg bytes)Gapes
I've found the issue already. It was a result of me including a header file with struct containing arrays that were too large.Toritorie
L
19

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

Leavis answered 25/2, 2017 at 5:43 Comment(0)
T
2

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.

Toritorie answered 25/2, 2017 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.