-
Segmentation fault
HI again.
I was wondering if you guys could clue me in to something. I know that segmentation faults are caused when the program tries to access memory that is not allocated for the program. However, I have a function call that is causing a segmentation fault. When I run gdb, it says that there is a segmentation fault at the line where the function call is. I know its not inside the function because of what gdb says. I made sure that everything was being passed to the function correctly, and that all the variables were declared corectly.
Thanks
-
You can also get a segfault when you attempt to write to memory that is read protected, which may be the case when you go past your array bounds. If you want to try to replicate the code in a small example and post it here we can take a look at it. Hint: if it proves nearly impossible to replicate the problem in a smaller program, you probably have a memory bounds violation problem (buffer overflow) and you may want to consult a little writeup I did here: http://sol-biotech.com/code/BufferOverflows.html.
-
Post some code, and comment the line which is causing the segfault.
> When I run gdb, it says that there is a segmentation fault at the line where the function call is.
Eg.
int len = strlen(NULL);
gdb may not be able to step into the function which crashes, particularly for low level library functions like strlen say. So it will just point the cursor at the innermost line of code it does know about.
-
Its a function that I wrote.
The line is:
Code:
Create_data(addeff, data, net, -999);
the first 3 are structures passed by reference:
Code:
void Create_data(AEE &actual, datavar &data, total_net &net, double fake_st_dev);
-
Post ALL the code that replicates the problem. We can't read your code remotely! Have you taken the time to read my writeup?
-
What code would you like? All 2000 lines of it? From thte debugger I know this is what is causing the seg fault. The only other thing that happens in this file is that the variable are declared. If the seg fault was an array out of bounds then it would have told me it occured somewhere else. I've had seg faults before, and know that gdb stops at them, I've just never seen it happen from a function call.
-
If you don't want help, don't ask for it. No one here can help you without the code.
-
Better yet, try to distill the problem into a short code example that you can post easily. Often while doing that, you will discover the problem.
-
I didn't say I didn't way help. You asked for code, I don't know what code to post. Theres a lot of it. Do you want the code that calls the function, do you want the functions code. I don't know what is causing this error, so I don't know what code to post other than the code that caused the seg fault.
Also, I tried to do the same thing in a smaller function (pass the same data structures) and it worked fine. So I don't know what is wrong.
-
As I told you, I am 99.9% sure it is a buffer overrun somewhere in your code, hence the link to the writeup on diagnosing and fixing buffer overflows.
-
I read your article and my problem didn't seem to fit. I haven't gotten it to compile at all yet, so I haven't yet seen the problem of recompilation causes it to work or not work. And I don't have a variable getting trash data, I'm just getting a segmentation fault when I call a function. I don't see how this fits into your diagnosis
-
If you haven't got it to compile, how the heck can you possibly get a segfault?
-
Sorry, I wasn't clear. I haven't gotten it to compile and not have this seg fault. The first time I got it to compile I got this error. Sorry about the confusion.
-
For what it's worth, I don't think you should write more than a handful of lines without compiling to make sure it works. Trying to debug 2000 lines of code all at once would be a nightmare. I suggest you start commenting out all of the code but one functional section of the code. Compile that and test it to be sure it does what it should do. Then add another functional section of code and test it. When you add back a section of code that causes the problem go through it line by line. If that doesn't do it , try writing a smaller program that does what the implicated section does in a smaller, isolated program and if that doesn't help you find the problem, then post the implicated section of code. It can be a tedious process, but if you can't figure out what the problem is, or where the problem is, otherwise, this technique will frequently help you find it, or at least narrow your search down to a manageable size so you can communicate successfully with someone else to help you find the problem.
-
Doing this is what I would normally do. I did this a bit differently because I wasn't really thinking. Its not really 2000 lines of undebugged code. There are some debugged parts, but I could probably break it down and go function by function. Probably the best plan for making sure I get the correct data.
However, I still wonder how a function call could end up in a seg fault.