释放结构 [英] freeing structure
问题描述
如果我有以下结构并执行以下程序,那么
我在结束时是否正确释放了所有已分配的结构内存?
typedef struct {
双重名称;
double * point2sumthing1;
double * point2sumthing2;
} obj_t;
obj_t * obj1 =(obj_t *)malloc(sizeof(obj_t));
obj1-> ; point2sumthing1 =(double *)malloc(5 * sizeof(double));
obj2-> point2sumthing2 =(double *)malloc(10 * sizeof(double));
....(做一些事情)
....
免费(obj1);
我关心的是两个双指针。我不确定free()
是否将它们视为结构的一部分
谢谢
wanwan写道:如果我有以下结构并执行以下程序,将
我正确释放所有分配的内存最后的结构是什么?
typedef struct {
双名;
double * point2sumthing1;
double * point2sumthing2;
} obj_t;
obj_t * obj1 =(obj_t *)malloc(sizeof(obj_t));
obj1-> point2sumthing1 =(double *)malloc( 5 * sizeof(double));
obj2-> point2sumthing2 =(double *)malloc(10 * sizeof(double));
...(做一些东西)
...
免费(obj1);
我关心的是两个双指针。我不确定free()
是否将它们视为结构的一部分
这种方法不起作用。
你必须单独释放双指针。
喜欢,
免费(obj1-> point2sumthing1);
免费(obj1-> point2sumthing2);
免费(obj1 )
否则会有内存泄漏
谢谢
"弯弯" < ER ******* @ yahoo.com>写道:
[三个malloc()s,一个free()]如果我有以下结构并执行以下程序,将
我在结束时正确释放了所有分配的结构内存?
编号一般来说,malloc()和free()应该配对。如果从malloc()获得
a指针,那么你需要自己释放()它。
-
Ben Pfaff >
电子邮件: bl*@cs.stanford.edu
web: http://benpfaff.org
thx进行回复。
如果我在指向NULL时自由(obj1-> point2sumthing1)怎么样?
它会给我一个运行时错误吗?
if I have the following structure and do the following procedure, will
I properly free up all allocated memory of the struture at the end?
typedef struct {
double name;
double *point2sumthing1;
double *point2sumthing2;
} obj_t;
obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));
obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));
obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));
....(do some stuff)
....
free(obj1);
My concern is the two double pointers. I am not certain whether free()
treats them as part of the structure
Thanks
wanwan wrote:if I have the following structure and do the following procedure, will
I properly free up all allocated memory of the struture at the end?
typedef struct {
double name;
double *point2sumthing1;
double *point2sumthing2;
} obj_t;
obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));
obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));
obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));
...(do some stuff)
...
free(obj1);
My concern is the two double pointers. I am not certain whether free()
treats them as part of the structure This approach doesnt work.
You have to free the double pointers seperately.
like,
free(obj1->point2sumthing1);
free(obj1->point2sumthing2);
free(obj1)
Otherwise there will be memory leak
Thanks
"wanwan" <er*******@yahoo.com> writes:
[three malloc()s, one free()]if I have the following structure and do the following procedure, will
I properly free up all allocated memory of the struture at the end?
No. In general malloc() and free() should be paired. If you get
a pointer from malloc(), then you need to free() it yourself.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
thx for responding.
How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?
这篇关于释放结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!