簡體   English   中英

C-釋放Malloc。 運行時出錯

[英]C - Freeing Malloc. Getting error while running

我不知道我的程序出了什么問題。 我使用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()分配的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()更改該原始字符串,用空字符替換分隔符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM