gpt4 book ai didi

C - 释放 Malloc。运行时出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:26:28 24 4
gpt4 key购买 nike

我不知道我的程序有什么问题。我使用循环使用 malloc 分配内存,当我释放它时,它给了我以下错误:

"*** glibc detected *** ./assignment4: free(): invalid pointer: 0x08eeb196 ***

此错误伴随着一个列表,该列表具有标题内存映射。循环从 1 运行到 7。奇怪的是,当我释放字符串的 [0] 值时,我没有收到任何错误。该错误仅在我尝试将 [1] 释放到使用循环分配的 [7] 时出现。

void lineParse()
{
int i;
FILE *fp;
fp = fopen("specification.txt", "r");

char ** listofdetails;
listofdetails = malloc(sizeof(char*)*6);

for(i=0;i<6;i++)
{
listofdetails[i] = malloc(sizeof(char)*40);
fgets(listofdetails[i], 40, fp);
/*printf("%s \n", listofdetails[i]);*/
/*free(listofdetails[i]);*/
}


char ** stringOne;
stringOne = malloc(sizeof(char*)*8);

stringOne[0] = malloc(sizeof(char)*6);
stringOne[0] = strtok(listofdetails[0], " ");

for(i=1;i<8;i++)
{
stringOne[i] = malloc(sizeof(char)*6);
stringOne[i] = strtok(NULL, " ");
}

char ** stringTwo;
stringTwo = malloc(sizeof(char*)*8);

stringTwo[0] = malloc(sizeof(char)*6);
stringTwo[0] = strtok(listofdetails[1], " ");

for(i=1;i<8;i++)
{
stringTwo[i] = malloc(sizeof(char)*6);
stringTwo[i] = strtok(NULL, " ");
}

char ** stringThree;
stringThree = malloc(sizeof(char*)*8);

stringThree[0] = malloc(sizeof(char)*6);
stringThree[0] = strtok(listofdetails[2], " ");

for(i=1;i<8;i++)
{
stringThree[i] = malloc(sizeof(char)*6);
stringThree[i] = strtok(NULL, " ");
}

char ** stringFour;
stringFour= malloc(sizeof(char*)*8);

stringFour[0] = malloc(sizeof(char)*6);
stringFour[0] = strtok(listofdetails[3], " ");

for(i=1;i<8;i++)
{
stringFour[i] = malloc(sizeof(char)*6);
stringFour[i] = strtok(NULL, " ");
}

char ** stringFive;
stringFive= malloc(sizeof(char*)*8);

stringFive[0] = malloc(sizeof(char)*6);
stringFive[0] = strtok(listofdetails[4], " ");

for(i=1;i<8;i++)
{
stringFive[i] = malloc(sizeof(char)*6);
stringFive[i] = strtok(NULL, " ");
}

char ** stringSix;
stringSix= malloc(sizeof(char*)*8);

stringSix[0] = malloc(sizeof(char)*6);
stringSix[0] = strtok(listofdetails[5], " ");

for(i=1;i<8;i++)
{
stringSix[i] = malloc(sizeof(char)*6);
stringSix[i] = strtok(NULL, " ");
}

printf(" %s \n", stringSixrows);*/

{ //This works fine
free(stringOne[0]);
free(stringTwo[0]);
free(stringThree[0]);
free(stringFour[0]);
free(stringFive[0]);
free(stringSix[0]);
}



for(i=1;i<8;i++) //But here is where the problem arises. If i remove this code is runs fine.
{
free(stringOne[i]);
free(stringTwo[i]);
free(stringThree[i]);
free(stringFour[i]);
free(stringFive[i]);
free(stringSix[i]);
}


free(listofdetails);
free(stringOne);
free(stringTwo);
free(stringThree);
free(stringFour);
free(stringFive);
free(stringSix);
fclose(fp);

}

最佳答案

让我们看看如何使用 strtok():

for(i=1; i<8; i++) {
stringOne[i] = malloc(sizeof(char)*6);
stringOne[i] = strtok(NULL, " ");
}

....

for(i=1; i<8; i++) {
free(stringOne[i]);
}

问题:

  1. 您非常清楚地泄漏了使用 malloc() 分配的内存。
  2. strtok() 本身不分配内存。
  3. 稍后在程序中您尝试释放 strtok() 返回的指针,这将导致未定义的行为。

很明显,您不了解 strtok() 的工作原理。因此,让我们尝试对其进行描述。

让我们取一个字符串:"hello world and people"

char[] s = "hello world and people";
char * token = strtok(s, " ");

// The memory as s is now: "hello\0world and people\0"
// token points to "hello\0world and people"

token = strtok(NULL, " ");

// The memory at s is now: hello\0world\0and people\0"
// token points to "world\0and people"

token = strtok(NULL, " ");

// The memory at s is now: hello\0world\0and\0people\0"
// token points to "and\0people"

token = strtok(NULL, " ");

// The memory at s is now: hello\0world\0and\0people\0"
// token points to "people"

token = strtok(NULL, " ");

// The memory at s is still: hello\0world\0and\0people\0"
// token points to NULL

请注意,在任何时候我们都没有分配任何额外的内存(除了原始字符串的空间,在这种情况下是在堆栈上分配的)。 strtok() 改变原始字符串,用空字符替换分隔符。

关于C - 释放 Malloc。运行时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com