C语言 glibc 检测到 malloc():C 中的内存损坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19057079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
glibc detected malloc(): memory corruption in C
提问by Hugh H
I am trying to compile and code written in C under linux, and got this error message:
我试图在linux下编译和用C编写代码,并收到此错误消息:
glibc detected malloc(): memory corruption
glibc 检测到 malloc():内存损坏
and I cannot find out why...
我不知道为什么...
the substring() just return you part of the original string by giving the starting index and length. e.g. substring("this is example",0,4) = "this";
substring() 只是通过给出起始索引和长度来返回原始字符串的一部分。例如 substring("this is example",0,4) = "this";
char *substring(char* str, int start, int length) {
char *newString = (char *)malloc(length * sizeof(char));
int i, x = 0;
int end=start+length-1;
for(i = start ; i <= end; i++){
newString[x++] = str[i];
}
newString[x] = '//consoleCommand has the form of 'send MESSAGE ID', has the value from stdin
int firstSpace = getCharIndexFirst(consoleCommand,' ');
int lastSpace = getCharIndexLast(consoleCommand,' ');
int len = strlen(consoleCommand);
char *header = substring(consoleCommand,0,firstSpace);
printf("header is: %s\n",header);
char *cmd = substring(consoleCommand,firstSpace+1,lastSpace-firstSpace-1);
printf("command is: %s\n",cmd); // the code only runs up to here and output the error..
char *socketstr = substring(consoleCommand,lastSpace+1,len-lastSpace-1);
printf("socket is: %s\n",socketstr);
';
return newString;
}
and the getCharIndexFirst() just returns the index of first occurance of the specified char the getCharIndexLast() just returns the index of last occurance of the specified char
getCharIndexFirst() 只返回指定字符第一次出现的索引 getCharIndexLast() 只返回指定字符最后一次出现的索引
and below is the main function:
以下是主要功能:
newString[x] = 'char *newString = (char *)malloc((length + 1) * sizeof(char));
';
Here is more info: the consoleCommand is usually the stdin, has the form of 'send MESSAGE ID', the error occurs when the MESSAGE is 12 char long... e.g. 'send this message 4', 'this message' is the cmd and has length of 12 chars, this gives me error! and it works fine for any other lengths, i have tried 3, 4, 24...
这里有更多信息:consoleCommand 通常是标准输入,具有“发送消息 ID”的形式,当消息长度为 12 个字符时会发生错误......例如“发送此消息 4”,“此消息”是 cmd并且长度为 12 个字符,这给了我错误!它适用于任何其他长度,我已经尝试了 3、4、24...
Any hint will be appreciated, THANKS!
任何提示将不胜感激,谢谢!
回答by Katniss
At this point xis equal to length, which means you're writing 1 character beyond the end of the memory you allocated. You need to allocate space for one more character.
此时x等于length,这意味着您在分配的内存末尾写入 1 个字符。您需要为多一个字符分配空间。
回答by cdhowie
You don't allocate any space for the terminating '\0'character, so you overflow your allocation to write this character. You need to count this character in your allocation too:
您没有为终止'\0'字符分配任何空间,因此您将分配溢出以写入此字符。您还需要在分配中计算这个字符:

