如何正确地释放某些malloc的数组元素?

时间:2021-05-10 19:56:35

I'm using the following struct and methods:

我使用的结构和方法如下:

struct cell {
    double x, y, h, g, rhs;
    struct key *keys;
};

void cellFree(struct cell *c)   {
    free(c->keys);
    c->keys = NULL;
    free(c);
    c = NULL;
}

void cellCopyValues(struct cell *targetcell, struct cell *sourcecell)   {
    targetcell->x = sourcecell->x;  
    targetcell->y = sourcecell->y;  
    targetcell->h = sourcecell->h;  
    targetcell->g = sourcecell->g;  
    targetcell->rhs = sourcecell->rhs;  
    keyCopyValues(targetcell->keys, sourcecell->keys);
}

struct cell * cellGetNeighbors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km)  {
    int i;

    // CREATE 8 CELLS
    struct cell *cn = malloc(8 * sizeof (struct cell));

    for(i = 0; i < 8; i++)  {
        cn[i].keys = malloc(sizeof(struct key));
        cellCopyValues(&cn[i], c);
    }


    return cn;
}

struct cell * cellMinNeighbor(struct cell *c, struct cell *sstart, struct cell *sgoal, double km)   {
    // GET NEIGHBORS of c
    int i;
    struct cell *cn = cellGetNeighbors(c, sstart, sgoal, km);
    double sum[8];
    double minsum;
    int mincell;

    cellPrintData(&cn[2]);

    // *** CHOOSE A CELL TO RETURN
    mincell = 3; // (say)


    // Free memory
    for(i = 0; i < 8; i++)  {
        if(i != mincell)    {
            cellFree(&cn[i]);
        }
    }

    return (&cn[mincell]);
}

When I call cellMinNeighbor() I need to return one of the 8 spawned neighbors (from cellGetNeighbors()) based on a selection criteria - however, the current method which I've applied to the free the other elements seems to be giving me the following error:

当我调用cellMinNeighbor()时,我需要根据选择条件返回8个衍生邻居中的一个(来自cellgetneighbor()))——然而,我对free应用的当前方法,其他元素似乎给了我以下错误:

*** glibc detected *** ./algo: free(): invalid pointer: 0x0000000001cb81c0 ***

What am I doing wrong? Thanks.

我做错了什么?谢谢。

1 个解决方案

#1


5  

You are allocating an array and then trying to free particular members.

您正在分配一个数组,然后尝试释放特定的成员。

Your cn is allocated to be an array of 8 struct cell, but you are actually trying to free &cn[0], &cn[1], &cn[2], which haven't actually been allocated using a malloc which requires it's own free.

你的cn被分配为一个8个struct单元的数组,但是你实际上是在尝试*&cn[0], &cn[1], &cn[2],它实际上并没有使用malloc来分配,这需要它自己的*。

You should free only those pointers you got by malloc and one good rule to remember is that the number of frees must correspond to the number of mallocs.

您应该只释放malloc提供的那些指针,需要记住的一个好规则是,释放的数量必须与mallocs的数量相对应。

In this case, you malloc cn and the individual keys, but not &cn[1] etc. So freeing them is a mistake.

在这种情况下,您malloc cn和单独的键,而不是&cn[1]等。因此释放它们是错误的。

If you count the mallocs, you have 9, but frees are 16.

如果你数了mallocs,你有9,但释放是16。

#1


5  

You are allocating an array and then trying to free particular members.

您正在分配一个数组,然后尝试释放特定的成员。

Your cn is allocated to be an array of 8 struct cell, but you are actually trying to free &cn[0], &cn[1], &cn[2], which haven't actually been allocated using a malloc which requires it's own free.

你的cn被分配为一个8个struct单元的数组,但是你实际上是在尝试*&cn[0], &cn[1], &cn[2],它实际上并没有使用malloc来分配,这需要它自己的*。

You should free only those pointers you got by malloc and one good rule to remember is that the number of frees must correspond to the number of mallocs.

您应该只释放malloc提供的那些指针,需要记住的一个好规则是,释放的数量必须与mallocs的数量相对应。

In this case, you malloc cn and the individual keys, but not &cn[1] etc. So freeing them is a mistake.

在这种情况下,您malloc cn和单独的键,而不是&cn[1]等。因此释放它们是错误的。

If you count the mallocs, you have 9, but frees are 16.

如果你数了mallocs,你有9,但释放是16。