Thread: How can I free what strtok returns?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Question How can I free what strtok returns?

    Hello all,

    If I malloc a char* then set that to the return value of strtok, how do I safely free that original memory? Below is code that does what I like, however I know there's a big memory leak here when I don't free (subString), however when I do it SIGABRTs, because at that point, subString no longer points to what I originally malloc'ed it to.

    Any ideas? Other than making subString static I'm not sure how to get around this.

    Thanks as always.

    p.s. I tried to strip down my code to illustrate what I'm doing:
    Code:
    Boolean ParseMenuForFilenameOption(const char *result, int *option, const char *filename)
    {
       char *subString;         /* copy of result we can alter locally */
       char *supposedFilename;  /* used to compare our subString with filename */
       char *line;              /* line must ONLY point to something within subString, or NULL! */
       int  value;              /* temp holder for potential 'option' value */
    
       supposedFilename = (char *) malloc(sizeof(char) * 1024);
       strcpy(supposedFilename, "");
    
       subString = (char *) malloc(sizeof(char) * strlen(result));
    
       /* copy our menu into local variable so we can safely alter it */
       strcpy(subString, result);
    
       /* now get our first token */
       line = strtok(subString, "\n");
    
       while (line != NULL)
       {
          /* see if what we're dealing with is a dataline, header or footer */
          if (sscanf(line, "%d-%s", &value, supposedFilename) == 2)
          {
             /* found it */
             *option = value;
    	 free (subString);
    	 free (supposedFilename);
             return (TRUE);
    
          } /* end if sscanf */
    
          /* now get the next line of data */
    
          line = strtok(NULL, "\n");
    
       } /* end while */
    
       free (supposedFilename);
       free (subString);
       return (FALSE);
    
    } /* end ParseMenuForFilenameOption(...) */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do realize that you're double-free()-ing your pointers there, right? You free them once in the loop, and again outside of it.

    More than likely, that's your whole problem. You need only one free call per matching malloc. Not two.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    41

    Thumbs up

    Thanks.
    (I was using C but wasn't aware of the casting issues for malloc, thanks for the pointers)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Salem
    > You do realize that you're double-free()-ing your pointers there, right?
    Both are followed by return (true or false)
    So a double free isn't the cause
    Man I should just stop posting today. I'm out of it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free() doesn't seem to work...
    By AlienJedi in forum C Programming
    Replies: 10
    Last Post: 01-29-2008, 05:27 PM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM