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



我不知道我的程序出了什么问题。我使用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: "helloworld and people"
// token points to "helloworld and people"
token = strtok(NULL, " ");
// The memory at s is now: helloworldand people"
// token points to "worldand people"
token = strtok(NULL, " ");
// The memory at s is now: helloworldandpeople"
// token points to "andpeople"
token = strtok(NULL, " ");
// The memory at s is now: helloworldandpeople"
// token points to "people"
token = strtok(NULL, " ");
// The memory at s is still: helloworldandpeople"
// token points to NULL

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

相关内容

  • 没有找到相关文章