glibc检测到malloc(): C中的内存损坏。

时间:2023-01-13 19:25:32

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] = '\0';
    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()只返回指定char的第一次出现的索引,getCharIndexLast()只返回指定char的最后发生的索引。

and below is the main function:

下面是主要的功能:

//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);

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...

这里有更多的信息:康索曼通常是stdin,有“发送消息ID”的形式,当消息是12字符长时出现错误……如。“发送这个消息4”,“这个消息”是cmd,长度是12个字符,这给我错误!它适用于任何其他长度,我试过3 4 24…

Any hint will be appreciated, THANKS!

任何提示都可以,谢谢!

2 个解决方案

#1


12  

newString[x] = '\0';

At this point x is 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等于长度,也就是说,你在你分配的内存的末尾写了一个字符。你需要为一个角色分配空间。

#2


5  

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”字符分配任何空间,因此您会溢出您的分配来编写这个字符。你也需要在你的分配中计算这个字符:

char *newString = (char *)malloc((length + 1) * sizeof(char));

#1


12  

newString[x] = '\0';

At this point x is 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等于长度,也就是说,你在你分配的内存的末尾写了一个字符。你需要为一个角色分配空间。

#2


5  

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”字符分配任何空间,因此您会溢出您的分配来编写这个字符。你也需要在你的分配中计算这个字符:

char *newString = (char *)malloc((length + 1) * sizeof(char));