使用malloc'd成员释放malloc'd结构 [英] free'ing malloc'd structure with malloc'd members

查看:74
本文介绍了使用malloc'd成员释放malloc'd结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任务过程中,我学会了很难的方法,我不应该尝试获得一个malloc''d结构的malloc''d成员释放后<$
br $>
结构(即免费(结构);免费(结构 - >缓冲空间))。


我的问题是,如果我只释放结构,将会(例如)缓冲空间是否可以隐含地释放
,或者我必须(我目前正在)释放会员

首先?


谢谢。

-cjl

In the course of an assignment, I learned the hard way that I shouldn''t try
to free a malloc''d member of a malloc''d structure after having freed that
structure (i.e., free( structure ); free( structure->bufferspace ) ).

My question is, if I free just the structure, will the (e.g.) bufferspace be
freed implicitly, or do I have to (as I currently am) free the members
first?

Thanks.
-cjl

推荐答案

John写道:
在课程中在一项任务中,我学会了一种艰难的方式,即在释放出结构(即自由结构)之后,我不应该尝试释放malloc'结构的malloc'成员。 ); free(structure-> bufferspace))。

我的问题是,如果我只释放结构,那么(例如)缓冲区空间是否会被隐式释放,或者我是否必须(就像我目前一样am)首先释放成员吗?
In the course of an assignment, I learned the hard way that I shouldn''t try
to free a malloc''d member of a malloc''d structure after having freed that
structure (i.e., free( structure ); free( structure->bufferspace ) ).

My question is, if I free just the structure, will the (e.g.) bufferspace be
freed implicitly, or do I have to (as I currently am) free the members
first?




首先释放成员。关于后续分配

成员的信息并没有神奇地包含在有关

结构分配的信息中。



Free the members first. Information about subsequent allocation of
members is not magically incoded into the information about the
structure allocation.


John在30/07/04写道:
John wrote on 30/07/04 :
在作业过程中,我学到了很难的方法,我不应该尝试释放malloc的malloc成员在释放了
结构(即自由(结构);自由(结构 - >缓冲空间))之后的结构。

我的问题是,如果我只释放结构, (例如)缓冲区空间是否会被隐式释放,或者我必须(我目前正在)释放成员
In the course of an assignment, I learned the hard way that I shouldn''t try
to free a malloc''d member of a malloc''d structure after having freed that
structure (i.e., free( structure ); free( structure->bufferspace ) ).

My question is, if I free just the structure, will the (e.g.) bufferspace be
freed implicitly, or do I have to (as I currently am) free the members
first?




所有已完成的事情必须撤消。


现在,是的,你必须首先释放最内层的元素。没有

隐式自动机制可以释放C中的内存。


也就是说,你可以编写一对称为''creator / <的函数br />
析构函数''有助于正确创建/删除对象并隐藏

用户不应该处理的一些血腥细节。


myobj_s * myobj_create(void);

void myobj_delete(myobj_s * this);


-

Emmanuel

C-FAQ: http ://www.eskimo.com/~scs/C-faq/faq.html


C是一个敏锐的工具




Martin Ambuhl写道:


Martin Ambuhl wrote:
John写道:
在任务过程中,我学会了一种艰难的方式,我不应该尝试释放一个malloc''d结构的malloc''d成员后释放它
st结构(即自由(结构); free(structure-> bufferspace))。

我的问题是,如果我只释放结构,那么(例如)缓冲区空间是否会被隐式释放,或者我是否必须(如我目前是)先释放会员
In the course of an assignment, I learned the hard way that I shouldn''t try
to free a malloc''d member of a malloc''d structure after having freed that
structure (i.e., free( structure ); free( structure->bufferspace ) ).

My question is, if I free just the structure, will the (e.g.) bufferspace be
freed implicitly, or do I have to (as I currently am) free the members
first?



首先释放会员。有关后续分配
成员的信息并没有神奇地包含在有关
结构分配的信息中。



Free the members first. Information about subsequent allocation of
members is not magically incoded into the information about the
structure allocation.




啊,但是如果有其他结构怎么办?还指着其中一个成员?确保

你保留指向该成员的项目数量,并且当你可以释放它直到它达到0时减少

,然后释放它?...



Ah, but what if some other structure is also pointing to one of those members? Do
you keep a count of the number of items pointing to that member, and decrement
when you might free it until it reaches 0, then free it?...


这篇关于使用malloc'd成员释放malloc'd结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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