Free() :无效的下一个尺寸(快速)错误
Free() : invalid next size (fast) error
所以我保留 运行 这个错误:free(): invalid next size(fast)
当我 运行 我的代码。如果我删除函数末尾的 free,我知道我正在泄漏内存,但我不明白为什么会出现此错误。
我认为这与我错误地分配内存有关,但我似乎找不到解决方法,这是我的代码:
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 是 space,CRLF 是 return,换行。
改变这个:
char* copy = malloc(sizeof(line));
对此:
char* copy = malloc(strlen(line) + 1);
第一个为line
的大小分配space,这是一个指针!
而第二个,分配 space 等于 line
指向的字符串的长度,加一个 ,用于 NULL 终止符(请不要忘记这一点,你会过上更幸福的 c-生活)! ;)
顺便说一句,我认为将代码注释写在代码行上方(而不是旁边)更为常见。 :)
在线:
char* copy = malloc(sizeof(line)); //allocate space for a copy of the line parameter
您正在分配内存来保存指针的大小。您需要改为分配字符串的长度。请参阅以下内容:
#include <stdio.h>
#include <string.h>
int main(int argc, const char* argv[]) {
const char *line = "this is a line";
printf("sizeof line: %zu\n", sizeof(line));
printf("strlen line: %zu\n", strlen(line));
return 0;
}
输出:
sizeof line: 8
strlen line: 14
您应该在 strlen+1 上分配(以解决空字符)。
所以我保留 运行 这个错误:free(): invalid next size(fast)
当我 运行 我的代码。如果我删除函数末尾的 free,我知道我正在泄漏内存,但我不明白为什么会出现此错误。
我认为这与我错误地分配内存有关,但我似乎找不到解决方法,这是我的代码:
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 是 space,CRLF 是 return,换行。
改变这个:
char* copy = malloc(sizeof(line));
对此:
char* copy = malloc(strlen(line) + 1);
第一个为line
的大小分配space,这是一个指针!
而第二个,分配 space 等于 line
指向的字符串的长度,加一个 ,用于 NULL 终止符(请不要忘记这一点,你会过上更幸福的 c-生活)! ;)
顺便说一句,我认为将代码注释写在代码行上方(而不是旁边)更为常见。 :)
在线:
char* copy = malloc(sizeof(line)); //allocate space for a copy of the line parameter
您正在分配内存来保存指针的大小。您需要改为分配字符串的长度。请参阅以下内容:
#include <stdio.h>
#include <string.h>
int main(int argc, const char* argv[]) {
const char *line = "this is a line";
printf("sizeof line: %zu\n", sizeof(line));
printf("strlen line: %zu\n", strlen(line));
return 0;
}
输出:
sizeof line: 8
strlen line: 14
您应该在 strlen+1 上分配(以解决空字符)。