所以我一直运行到这个错误:free(): invalid next size(fast)
当我运行我的代码。如果我在该函数结束时删除了自由,我知道我正在泄漏内存,但我不明白为什么我会收到此错误。免费():无效下一个大小(快)错误
我认为它有话跟我错误地分配内存,但我似乎无法找到修复,这里是我的代码:
bool parse(const char* line) //NOT WORKING JUST QUITE
{
char* copy = malloc(sizeof(line)); //allocate space for a copy of the line parameter
strcpy(copy, line); //copy the line parameter
char* method = strtok(copy, " "); //pointer to the method
char* reqLine = strtok(NULL, " "); //pointer to the requestline
char* version = strtok(NULL, "\r\n"); //pointer to the HTTP-Version
if (strcmp(method,"GET") != 0) //if the method is not GET
{
printf("%s\n", method);
printf("ERROR 405\n");
return false;
}
if (strncmp(reqLine, "/", 1) != 0)//if the request line does not begin with a/character
{
printf("%c\n", reqLine[0]);
printf("%s\n", reqLine);
printf("ERROR 501\n");
return false;
}
if (strchr(reqLine, 34) != NULL) //if the request line contains a " 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;
}
如果它有助于在const char*
线传递是的形式:
method
SP request-target SP HTTP-version CRLF
其中SP是一个空格,CRLF是回车,换行。
您在某处分配内存。然后你写出那个记忆的界限。这是像这样的错误的唯一原因。 –
我不认为这是这种情况@JoachimPileborg,我发布了一个答案。我认为他的'malloc()'不是它应该的! – gsamaras
另外,当你有一个指针时,那个指针上的'sizeof'会给你指针*的大小*,而不是它指向的内容。在32位系统中,它很可能是“4”,而在64位系统中它很可能是“8”。 –