Segmentation fault

This is a discussion on Segmentation fault within the C++ Programming forums, part of the General Programming Boards category; HI again. I was wondering if you guys could clue me in to something. I know that segmentation faults are ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    48

    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

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    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.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    48
    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);

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Post ALL the code that replicates the problem. We can't read your code remotely! Have you taken the time to read my writeup?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    48
    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.

  7. #7
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    If you don't want help, don't ask for it. No one here can help you without the code.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    48
    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.

  10. #10
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    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.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    48
    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

  12. #12
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    If you haven't got it to compile, how the heck can you possibly get a segfault?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  13. #13
    Registered User
    Join Date
    May 2005
    Posts
    48
    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.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    48
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21