我如何正确释放某些malloc分配的数组元素? [英] How do I properly free certain malloc'd array elements?

查看:224
本文介绍了我如何正确释放某些malloc分配的数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的结构和方法:

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]);
}

当我称之为 cellMinNeighbor()我需要返回8催生邻国之一(从 cellGetNeighbors() )的基础上选择的标准 - 然而,我申请了免费的其他元素目前的方法似乎是给我下面的错误:

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:

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

我是什么做错了吗?谢谢你。

What am I doing wrong? Thanks.

推荐答案

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

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

CN 分配是数组8 结构单元,但你实际上是试图释放&放大器; CN [0],&安培; CN [1],&安培; CN [2] ,它并没有真正使用它需要它自己的自由一个malloc分配

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.

您应该释放只有那些你由malloc和一个好的规则要记住拿到三分是的FreeS的数量必须符合mallocs的数量。

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 CN 和个人密钥,而不是&放大器; CN [1] 等所以解放出来是错误的。

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

如果你算mallocs,你有 9 ,但释放是 16

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

这篇关于我如何正确释放某些malloc分配的数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆