Segmentation fault

Working on “The two piles difference” community puzzle and this error pops up when I try to run my C program.

Errors
Segmentation fault.
at Answer.c. function compute (list=0x7fff00000011) on line 71
at Answer.c. function main () on line 132

If I turn on DEBUG (a #define which generates a bunch of messages to stderr), the program runs fine, passes all the tests and the validation. Turn off DEBUG and it blows up.

Do you code in the IDE directly or do you copy/paste your code into it ? Because if you use copy/paste, sometimes the IDE doesn’t see your code. Add/remove a simple letter fix the bug.

I code directly in the IDE. I’ve tried adding and deleting stuff and mostly it just makes it worse. So far the only version that passes all the tests also generates the most debug output.

I posted a bug here a couple of days ago. I just checked to see if anyone had responded, and someone had. Great, but why was I not notified? (Problem is segmentation fault in “Two piles difference”)

Got to wondering if maybe I had made an error, so I did some more checking. Stripped out everything that had no effect and what I am left with is one fprintf to stderr in a computation routine that is called from the bottom of a stack of recursion calls. Comment out this one line and I get a segmentation fault, leave it in and the program runs fine. Even moving the line to a different location (just after the declarations in this same procedure) causes the program to blow up.

Tried running the program on my Linux box and the same thing happens. I think I am going try stackoverflow, see if anyone there has any ideas.

You can also try to run your program through Valgrind. It often helps to identify the cause of memory errors.

First thought: you forgot to initialise a variable. Especially since it works in DEBUG.

Otherwise, could you share some code?

A little work with gdb and I found the problem. I was overrunning a local array because I wasn’t checking the array index after incrementing. And here I thought I was the master of check-everything-all-the-time. I must be slipping.

2 Likes