strdup

This is a discussion on strdup within the C Programming forums, part of the General Programming Boards category; I'm having a few problems with memory leaks in my linked list. I think the problems may be with the ...

  1. #1
    Registered User hankspears's Avatar
    Join Date
    Mar 2002
    Posts
    14

    strdup

    I'm having a few problems with memory leaks in my linked list. I think the problems may be with the strdup() function. We've been given this so I don't know how it works. I looked at the old entries on the board, and found this link, which was quite useful:
    http://www.mkssoftware.com/docs/man3/strdup.3.asp
    However, I'm not sure what I have to free. I tried the following:

    Code:
    /* for information */
    typedef struct _Node {
        Contents *      item;
        struct _Node *  next;
    } Node;
    typedef Node* List;
    List    list;
    typedef char Contents;
    List    ListPrepend(List list, Contents * item);
    [...]
    
      char  str_contents[200];
      scanf("%s", str_contents);
      list = ListPrepend(list, strdup(str_contents));
      free(str_contents);
    I don't really understand what strdup is doing here. Why can't I just send str_contents to ListPrepend?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strdup() alloc's memory, and copies a string into it. A pointer to the new memory is returned. It is up to the caller, ie your code, to free that memory in an orderly fashion.

    In your code, you are strdup'ing the buffer so that you can reuse the buffer for the next lot of input. The new pointer is added to a struct, then the struct to the link list.

    You only need to free the memory when you are finished with it. You should have a function that goes through the link list, performing free's on each element as it goes. If you haven't got one of these, try coding one up, and post it here if you have problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User hankspears's Avatar
    Join Date
    Mar 2002
    Posts
    14
    I have a function that goes like this:
    Code:
    List ListRemove(List list, Contents * item)
    {
        Node *searchNode, *prevNode;
    
        /* look at each node until found item */
        for(searchNode = ListFront(list), prevNode = NULL;
            searchNode != NULL && NodeContents(searchNode) != item;
            prevNode = searchNode, searchNode = NodeNext(searchNode));
    
        /* item not found */
        if(searchNode == NULL) return list;
    
        /* item is first node */
        if(prevNode == NULL) list = NodeNext(list);
    
        /* item is another node */
        else prevNode->next = NodeNext(searchNode);
    
        free(searchNode);
            
        return list;
    }
    is this OK?
    If I run the program with bcheck, and enter n items into the list, then quit, I get 2*n memory leaks. If I remove the items in the list, I get n memory leaks. The bcheck .err file seems to point at strdup as the culprit, so I thought that may be the problem.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    That function doesn't free the contents of the node, just the memory allocated to the node structure itself.

    Either:

    a) free the contents of node in the function

    or

    b) free the contents of the node elsewhere.

    Which you do depends on what you're program does and how you want it to work. If you never use the contents once the node to which it belongs is removed from the list then free the contents in the function. If it's used elsewhere, whether it's in the list or not, then free it when you've finished with it (and it's been removed from the list).

    Ian Woods

  5. #5
    Registered User hankspears's Avatar
    Join Date
    Mar 2002
    Posts
    14
    Great, no memory leaks now.
    Thanks to my hirsute friend for sorting that one out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-11-2007, 09:40 AM
  2. strcpy, strdup
    By shibu in forum C Programming
    Replies: 6
    Last Post: 09-13-2006, 12:29 PM
  3. Question to do with strdup()
    By John.H in forum C Programming
    Replies: 9
    Last Post: 01-29-2003, 09:30 AM
  4. function: strdup()
    By cjtotheg in forum C Programming
    Replies: 3
    Last Post: 02-02-2002, 09:08 AM
  5. strcpy and strdup?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-26-2001, 06:46 AM

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