Reputation: 19568
char *str = malloc (14);
sprintf(str, "%s", "one|two|three");
char *token1, *token2, *token3;
char *start = str;
token1 = str;
char *end = strchr (str, '|');
str = end + 1;
end = '\0';
token2 = str;
end = strchr (str, '|');
str = end + 1;
end = '\0';
...
free(start);
does that free work properly since I have been setting bytes within str to null in order to tokenize it?
Upvotes: 1
Views: 160
Reputation: 15925
the free doesn't check the contents of the data. So yes this is correct
Upvotes: 3
Reputation: 347216
Yes it works, free does not care where the null termination is. Or even if there is one. You can use malloc/free for any type of data not only null terminated strings.
Upvotes: 5