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:
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);
subString = (char *) malloc(sizeof(char) * strlen(result));
/* copy our menu into local variable so we can safely alter it */
/* 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;
} /* end if sscanf */
/* now get the next line of data */
line = strtok(NULL, "\n");
} /* end while */
} /* end ParseMenuForFilenameOption(...) */