Thread: free()-ing after strtok()?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    free()-ing after strtok()?

    Hi, i'm writing a program and having some major memory problems(segfault in malloc, malloc_consolidate, SIGABRT in kernel_vsyscall bleh), so I figure it has to do with how I'm freeing memory. So I have no idea when to properly free something for example:

    Code:
    char* line;
    char* token;
    
    line =someFunctionThatMallocsAndReturnsAStringOfCrap();
    
    token = strtok(line," ");
    
    //blah blah
    
    token = strtok(0, " ");
    
    //free(line); or free(token);???
    Do i free each token after I'm finished with it, or do i free the line after i'm done tokenizing it or something else?

    thanks
    Last edited by Salem; 12-22-2007 at 04:28 AM. Reason: Use code tags, not php tags

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you manually allocate it (ie. malloc(), calloc(), etc. etc..), then you have to manually deallocate it. Otherwise, you only have to deallocate it if the specification for a function says that it is returning dynamically allocated memory that needs to be deallocated by you.

    Other than that, you're trying to free memory your program is not legally able to free.

    Calling free() on token in your example is completely wrong. If you are confused with the difference between free()ing line vs token, then you seriously need to review dynamic memory allocation.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    I've tried freeing the line after I've finished getting and using all the tokens and I get an error because of an invalid pointer at the function call free(). What am i doing wrong?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by alwut View Post
    I've tried freeing the line after I've finished getting and using all the tokens and I get an error because of an invalid pointer at the function call free(). What am i doing wrong?
    You could be doing all sorts of things wrong. First, perhaps you should verify the value of "line" just after the allocation [after someFunctionThatMallocsAndReturnsAStringOfCrap], and then again just before free. If free says it's an invalid pointer, then it's most likely that you have "lost" or "damaged" the pointer somewhere along time way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on free() ing a hash table
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 10-26-2007, 01:16 PM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  4. How can I free what strtok returns?
    By registering in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 04:56 PM