释放一个结构......我不确定我是否做得对

时间:2015-01-12 16:55:30

标签: c pointers linked-list structure free

我的程序管理结构的链接列表。

这是我的结构:

typedef struct wagon wagon;
typedef struct wagon{
    wagon *next;
    marchandise_ptr *liste;
    double volume_courant;
}train_ptr;

其中wagon * next是指向我的链表的下一个“单元格”的指针,marchandise_ptr * list是指向另一个链表的指针。为了解放我的结构,我按照以下步骤进行:

在int main()中:

train_ptr *train=creer_un_train(...)//so train is the beginning of my linked list
liberer_train(&train);

我的职能是:

创造一个“旅行车”

wagon *creer_wagon(marchandise_ptr *liste,double volume_courant){ //it creates a wagon
    assert(volume_courant>=0);
    wagon *wag=malloc(sizeof(wagon));
    if(wag==NULL)
        return NULL;
    wag->next=NULL;
    wag->volume_courant=volume_courant;
    wag->liste=liste;
    return wag;
}

在链接列表的末尾添加创建的“旅行车”:

train_ptr *ajouter_wagon_a_la_fin_du_train(train_ptr *train,double volume_courant, marchandise_ptr *liste){
    wagon *wag=creer_wagon(liste,volume_courant);
    if(wag==NULL)
        return NULL;
    if(train==NULL)
        train=wag;
    else{
        train_ptr *wag_current=train;
        while(wag_current->next!=NULL)
            wag_current=wag_current->next;
        wag_current->next=wag;
        }
    return train;
}

创建一列火车:

train_ptr *creer_un_train(unsigned int nombre_de_wagons,marchandise_ptr *liste){
    assert(nombre_de_wagons>=0);
    int i;
    train_ptr *train=NULL;

    for(i=0;i<nombre_de_wagons;i++){
        train=ajouter_wagon_a_la_fin_du_train(train,rand()%10,liste);
        if(train==NULL)
            return NULL;
        }
    return train;
}

免费搭乘火车:

void liberer_train(train_ptr **train){
    train_ptr *p_current = *train;
    while(p_current!=NULL){
                *train = p_current->next;
                p_current->next=NULL;
                free(p_current->liste);
                free(p_current);
                p_current = *train;
    }
}

P.S。:liste是指向链表的beginnig的指针:

typedef struct marchandise marchandise;
typedef struct marchandise{
    double volume;
    double volume_total;
    char nom;
    marchandise *suivant;
}marchandise_ptr;

感谢您的关注! (对不起我的英语,我不是母语人士......:D)

1 个答案:

答案 0 :(得分:1)

creer_wagon函数中,liste函数似乎不应释放liberer_train,因为creer_wagon函数未分配creer_wagon 1}}功能。

遵循该逻辑,对liste进行调用的函数应该对free成员负责,因为在调用函数的作用域中你将有一个指向它的有效指针,并且您冒着liste成员加倍train的风险。

如果每个liste只需要引用typedef struct wagon{ wagon *next; const marchandise_ptr *liste; double volume_courant; }train_ptr; ,但不需要修改它,则可以像这样定义结构

free

这可以防止意外尝试修改或liste liste成员。

如果许多列车可以指向相同的struct wagon,这种设计是有意义的,尽管它增加了liste用户的责任,因为他们应该负责const的内存管理会员如果是这种情况,我会推荐liste限定符。

在我看来,这是一种有效的方式来推断是否liberer_train被你分配。

我建议你像这样修复void liberer_train(train_ptr **train) { train_ptr *p_current = *train; train_ptr *suivant = NULL; while (p_current != NULL) { suivant = p_current->next; free(p_current); p_current = suivant; } /* make the pointer NULL, so it's not a dangling pointer in the caller function anymore. */ *train = NULL; } 函数

typedef struct wagon train_ptr;

而且,我建议你改变

typedef struct wagon *train_ptr;

typedef struct wagon train;

_ptr

例如,因为后缀train_ptr x;让人认为在x {{1}}中是指针,而不是。

相关问题