C语言 如何释放只包含指针的结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4915369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:43:43  来源:igfitidea点击:

How to free a struct that contains only pointers

cstructfreetypedef

提问by wonnie

I have a struct which you see below:

我有一个你在下面看到的结构:

typedef struct _List {
    Person *person; // pointer for people list
    DoList *do; // Kinda timer, for checking list in some intervals
} List;

Are there any need to free this struct? If so, how can i free it?

有没有必要释放这个结构?如果是这样,我该如何释放它?

回答by par

You have to free the struct if you allocated it dynamically. You have to free its members before deallocating the struct if you allocated the members dynamically and don't have a reference to them anywhere else.

如果动态分配结构,则必须释放它。如果您动态分配成员并且在其他任何地方都没有对它们的引用,则必须在释放结构之前释放其成员。

Here are some examples:

这里有些例子:

void freeNotRequiredHere() {
    List            nonDynamicList;
    Person          nonDynamicPerson;
    DoList          nonDynamicDoList;

    nonDynamicList.person = &nonDynamicPerson;
    nonDynamicList.do = &nonDynamicDoList;
}


void freeRequiredForStructListOnly() {
    List            *dynamicList;
    Person          nonDynamicPerson;
    DoList          nonDynamicDoList;

    dynamicList = (List *) malloc( sizeof(struct List) );

    dynamicList->person = &nonDynamicPerson;
    dynamicList->do = &nonDynamicDoList;

    free( dynamicList );
}


void freeRequiredForStructListAndPersonOnly() {
    List            *dynamicList;
    Person          *dynamicPerson;
    DoList          nonDynamicDoList;

    dynamicList = (List *) malloc( sizeof(struct List) );
    dynamicPerson = (Person *) malloc( sizeof(Person) );

    dynamicList->person = dynamicPerson;
    dynamicList->do = &nonDynamicDoList;

    free( dynamicPerson );
    free( dynamicList );
}


void freeRequiredForStructListAndPersonOnly( DoList *notSureDoList ) {
    List            *dynamicList;
    Person          *dynamicPerson;

    dynamicList = (List *) malloc( sizeof(struct List) );
    dynamicPerson = (Person *) malloc( sizeof(Person) );

    dynamicList->person = dynamicPerson;
    dynamicList->do = notSureDoList;

    // maybe notSureDoList was allocated with malloc(),
    // maybe it is a non-dynamic stack variable.
    // the calling function should deal with free()'ing notSureDoList

    free( dynamicPerson );
    free( dynamicList );
}

回答by ayush

void free_mystruct(struct_List *a_ptr){
  free(a_ptr->person);
  free(a_ptr->do);
  free(a_ptr);
}

if you used malloc to initially allocate memory.

如果您使用 malloc 最初分配内存。

回答by DigitalRoss

It depends...

这取决于...

So you asked "...any need to free...?"and the answer is "it depends."

所以你问“......有没有必要释放......?” 答案是“视情况而定”。

  • If the struct is needed almost until the program terminates via a return from main(),exit(), or a signal, then noit should never be freed regardless of what is in it.1

  • If the resource is allocated dynamically in a long-lived process such as an editor or server daemon, but if after a transaction or period of time it is no longer needed, then yes, it does need to be freed or the program will have a memory leak.

  • Freeing the structure will produce a memory leak if the contained objects are also dynamically allocated. Either nothing at all should be freed or the entire graph of objects with a root at that structure will need to be freed.

  • 如果是通过从一个几乎回报所需的结构,直到程序终止main(),exit(),或信号,那么没有而且永远不应该不管它是什么释放。1

  • 如果资源是在诸如编辑器或服务器守护程序的长期进程中动态分配的,但是如果在事务或一段时间之后不再需要它,那么是的,它确实需要被释放,否则程序将有一个内存泄漏

  • 如果包含的对象也是动态分配的,则释放结构将产生内存泄漏。要么根本不应该释放任何东西,要么需要释放以该结构为根的整个对象图。

The rule is simple, each individual malloc()must correspond to a single individual free().

规则很简单,每个个体malloc()必须对应一个个体free()



1. Saying this generally attracts a small flood of doctrinaire "you must free everything"protest, but such protest is partly misinformed. The C++ Faq discussesthe issue well. One concern is that it's slow and pointless to page in or touch lots of pages that the OS is able to free as a block. But yes, there is an argument that it's a good design pattern, good practice, and if there is any possibility of incorporating the code into a second program then memory should be freed always.

1. 这句话一般会引起一小群教条主义“你必须解放一切”的抗议,但这种抗议在一定程度上是错误的。在C ++ FAQ讨论这个问题很好。一个问题是,分页或触摸操作系统能够作为一个块释放的大量页面是缓慢且毫无意义的。但是,是的,有一种观点认为这是一个很好的设计模式,很好的实践,如果有可能将代码合并到第二个程序中,那么应该始终释放内存。

回答by Oliver Charlesworth

If you have allocated an object using malloc(), then you need to free()it at some point.

如果您已经使用 分配了一个对象malloc(),那么您free()在某个时候需要它。