Thread: Doubly linked list-segmentation fault

  1. #1
    Registered User
    Join Date
    Mar 2012

    Doubly linked list-segmentation fault

    I have a doubly linked list data structure that abstracts some physical points in space. The code runs over time loops and calculations happen between member data of the nodes. I now have to add nodes to the dataset once in every n time steps. I am able to do this. But after a few time steps ( say a 100), I am getting a segmentation fault. GDB reveals that this happens when one of the functions tries to access a node data. This node data is explicitly assigned each time I am to add the nodes to the dataset. Why would this error happen after so many steps of successful execution? Any insights? My data structure has too many elements and the code is pretty big to post a code snippet here. Please let me know if anyone here has come across such an issue before and suggest me some direction to look at. Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    What you describe is quite common. And is generally an indication that some code, despite your belief that it is correct, is doing something wrong.

    I would guess code for handling some not-so-common edge case (eg adding a node to the start of the list, removing a node from the start or end of the list, walking through the list and searching for data that isn't actually in any node) is leaving some pointer in an invalid state. If you haven't realised you need to test for that edge case, it is quite common for symptoms to subsequently emerge when you assume things are working, and then start operating with a large number of nodes ..... which is exactly what you are describing.

    Bear in mind that symptoms of pointer molestation don't necessarily become visible immediately. It is quite common for code to do something wrong, but for execution to continue, with errors cascading, until the operating system finally detects your program reading or writing from memory it shouldn't. That means the problem is not necessarily in the code that "tries to access a node data". The problem (or potentially multiple problems) may exist in any of the code executed before that point is reached.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Have you tried using a debugger?

    gcc -g prog.c
    gdb ./a.out

    Or valgrind
    gcc -g prog.c
    valgrind ./a.out

    Or electric fence
    gcc -g prog.c -lefence
    gdb ./a.out

    All of these will give you more information than a single line at the end of a dead program.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with my doubly linked list
    By evildotaing in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2011, 11:47 AM
  2. Doubly Linked List Segmentation Fault
    By DaNxTh3xMaNx in forum C Programming
    Replies: 5
    Last Post: 09-09-2011, 09:50 AM
  3. Help with doubly linked list
    By Furbiesandbeans in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2008, 11:41 AM
  4. doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 03:41 AM
  5. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM

Tags for this Thread