Im trying to split a string according to the following rules:
- words without "" around them should be treated as seperate strings
- anything wiht "" around it should be treated as one string
However when i run it in valgrind i get invalid frees and invalid read size errors, but if i remove the two frees i get a memory leak. If anyone could point me in the right direction i would appreciate it
The code that calls split_string
char *param[5];
for(i = 0;i < 5;i++) {
param[i] = NULL;
}
char* user = getenv("LOGNAME");
char tid[9];
char* instring = (char*) malloc(201);
/
while((printf("%s %s >",user,gettime(tid)))&&(instring
=fgets(instring,201,stdin)) != NULL) {
int paramsize = split_string(param, instring);
The co开发者_Go百科de that tries to free param
for(i = 0;i < 5;i++) {
if(param[i] != NULL) {
free(param[i]);
fprintf(stderr,"%d",i);
}
}
int split_string(char** param, char* string) {
int paramplace = 0; //hvor vi er i param
int tempplace = 0; //hvor i temp vi er
char* temp = malloc(201);
int command = 0;
int message = 0;
for(; (*string != '\0') && (*string != 10) && paramplace < 4; string++) {
if((*string == ' ') && (message == 0)) {
if(command == 1) {
temp[tempplace] = '\0';
param[paramplace++] = temp;
tempplace = 0;
command = 0;
}
}
else {
if(*string =='"') {
if(message == 0) message = 1;
else message = 0;
}
if(command == 0) {
free(temp);
temp = malloc(201);
}
command = 1;
if(*string != '"') {
temp[tempplace++] = *string;
}
}
}
if(command == 1) {
temp[tempplace] = '\0';
param[paramplace++] = temp;
}
param[paramplace] = NULL;
free(temp);
return paramplace;
}
As far as I can see, you want to put the split strings into param
as an array of pointers (presumably making the caller responsible for freeing them). In the first branch of the if statement in your loop, you do so by assigning the current temp
buffer to that place. However, once you start a new string (when comnmand == 0
, you free that space, rendering the previous param
entry pointer invalid.
Only free each pointer once. I wouldn't rule out other leaks in this code: I think you can simplify your state machine (and probably find other bugs as a result).
When you free the temp buffer you also free the param[] buffer, where your tokens are stored. On the other hand, if you don't call free(temp)
, which you shouldn't, it will be the responsibility of the caller of your function to call free(param[n])
, when the tokens aren't needed.
Maybe you're not removing the right free()'s? The actual problem might be in the code that calls split_string. Can you show it?
It's hard to understand your code. I suggest you use sscanf instead.
You can use a format string like this:
"\"%[^\"]\"%n"
Read up on what it does.
I wrote an example:
if( sscanf( string, "\"%[^\"]\"%n", matchedstring, &bytesread ) )
{
handlestring( matchedstring );
string += bytesread;
}
else if( sscanf( string, "%s%n", matchedstring, &bytesread ) )
{
handlestring( matchedstring );
string += bytesread;
}
else
{
handleexception();
}
Untested. :)
Thanks to all the comments i found the answer. The problem was that the first malloc before the for loop is superfluous as there will be another one before it starts to put temp into param and therefore there were no pointers to the first malloc so it was simply lost.
精彩评论