Segmentation fault at free()

This is a discussion on Segmentation fault at free() within the C Programming forums, part of the General Programming Boards category; I'm getting a segmentation fault when I do free() in the delete function of the following linked list implementation. Please ...

  1. #1
    ddd
    ddd is offline
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Segmentation fault at free()

    I'm getting a segmentation fault when I do free() in the delete function of the following linked list implementation. Please take a look and tell me where I am going wrong.
    Code:
    typedef struct node {
        char name[100];
        int id;
        struct node* next;
    } Node;
    
    
    void insert(Node** p, char* _name, int _id)
    {
        Node *temp, *prev;
        temp = malloc(sizeof(struct node));
        temp->next = NULL;
        strcpy(temp->name,_name);
        temp->id = _id;
    
    
        if(*p == NULL) {
                *p = temp;
        }
        else {
                for(prev = *p; prev->next!=NULL; prev=prev->next);
                prev->next=temp;
        }
    }
    
    
    /* Delete entry
      @params p    first element
           _id     ID to delete
    */
    void delete_by_id(Node** p, int _id) {
        Node *temp, *prev;
        prev = NULL;
        for(temp = *p; temp!= NULL; prev = temp, temp=temp->next) {
                if(temp->id == _id) {
                    printf("Deleting entry with id: %d\n", temp->id);
                    if(prev == NULL)
                         *p = temp->next;
                    else
                         prev->next= temp->next;
                    free(temp);
                    return;
                }
        }     
    }
    When I run the program with valgrind, there is no seg. fault. It runs fine. So I am not able to figure out the problem.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,304
    Your code looks fine to me, and seems to run fine as well with the little wrapper program I wrote. Could you provide the smallest compilable set of code that exhibits the problem and the exact input you give to cause the seg fault?

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    segfaults on free are often double-free's (where you've tried to "free" something that's already been free'd) or where something was NEVER malloc'd in the first place. You're going to need to trace it in a debugger for a specific answer, unless you post the FULL code so that someone else can have a look in a debugger for you. Chances are, you never initialise something, you run off the end of your list of nodes and try to free memory that was never meant to be used, or you've somehow freed the same node twice.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault on Free()
    By Stiletto in forum C Programming
    Replies: 2
    Last Post: 01-25-2012, 02:34 PM
  2. Replies: 3
    Last Post: 03-12-2011, 07:28 PM
  3. segmentation fault (calloc, free)
    By kentadams in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 08:50 AM
  4. segmentation fault in free()
    By franziss in forum C Programming
    Replies: 4
    Last Post: 02-22-2005, 12:09 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


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