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.

jww
  • 97,681
  • 90
  • 411
  • 885
moosefoot
  • 133
  • 3
  • 11
  • What is line 35 in `src/prog.c`? What changes did you make? If you revert those changes does it work then? – Some programmer dude Feb 25 '17 at 05:31
  • 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. – moosefoot Feb 25 '17 at 05:34
  • Basics: does code `#include `, `#include `? – chux - Reinstate Monica Feb 25 '17 at 05:35
  • Even for small one-man projects, using a [*version control system*](https://en.wikipedia.org/wiki/Version_control_system) like [Git](https://git-scm.com/) or [Subversion](https://subversion.apache.org/) 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? – Some programmer dude Feb 25 '17 at 05:37
  • Code includes both of those. In fact, the code includes ``, ``, ``, ``, `` and ``. It also includes a header file I've made. – moosefoot Feb 25 '17 at 05:37
  • 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. – moosefoot Feb 25 '17 at 05:39
  • 6
    Can you provide a code which can reproduce this error and share with us? – lakshayg Feb 25 '17 at 05:39
  • Possible duplicate of [Segmentation Fault before main](http://stackoverflow.com/questions/20253267/segmentation-fault-before-main) – Dima Chubarov Feb 25 '17 at 05:55
  • 1
    How much data is your program placing on the stack? In Windows, the stack limit (by default) is only 1 meg bytes) – user3629249 Feb 25 '17 at 10:30
  • I've found the issue already. It was a result of me including a header file with struct containing arrays that were too large. – moosefoot Feb 25 '17 at 22:52

2 Answers2

17

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

Community
  • 1
  • 1
Squirrel
  • 369
  • 2
  • 12
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.

moosefoot
  • 133
  • 3
  • 11