c - Free() : invalid next size (fast) error -


so keep running error: free(): invalid next size(fast) when run code. if remove free @ end of function know leaking memory don't understand why getting error.

i assume has me allocating memory incorrectly can't seem find fix, here code:

bool parse(const char* line) //not working quite  {     char* copy = malloc(sizeof(line)); //allocate space copy of line parameter     strcpy(copy, line); //copy line parameter      char* method = strtok(copy, " "); //pointer method      char* reqline = strtok(null, " "); //pointer requestline     char* version = strtok(null, "\r\n"); //pointer http-version      if (strcmp(method,"get") != 0) //if method not     {         printf("%s\n", method);         printf("error 405\n");         return false;     }     if (strncmp(reqline, "/", 1) != 0)//if request line not begin / character     {         printf("%c\n", reqline[0]);         printf("%s\n", reqline);         printf("error 501\n");         return false;      }     if (strchr(reqline, 34) != null) //if request line contains " character     {         printf("%s\n", reqline);         printf("error 400\n");         return false;     }     if (strcmp(version, "http/1.1") != 0)     {         printf("%s", version);         printf("error 505\n");         return false;     }  //free(copy);  return true; } 

if helps passed in const char* line of form:

method sp request-target sp http-version crlf

where sp space , crlf carridge return, line feed.

change this:

char* copy = malloc(sizeof(line)); 

to this:

char* copy = malloc(strlen(line) + 1); 

the first allocates space size of line, pointer!

while second, allocates space equal length of string line points to, plus one, null terminator (please don't forget , live happier -life)! ;)


btw, believe it's more common write comments of code above line of code (rather next it). :)


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -