内存分配和释放内存 [英] memory allocation and freeing memory

查看:69
本文介绍了内存分配和释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我会使用第三方库,我会使用一些函数

返回char *或者结构等,有时会返回内存

库,当我尝试释放这个内存时,函数是免费的,

它会破坏我的应用程序,有时候它没问题,为什么呢?我如何实现

我必须释放由第三方库分配的内存和

为什么有时候我不必释放这些内存? />

谢谢

解决方案

Rodrigo Dominguez写道:

有时候我使用第三方库,我使用一些函数
返回char *或结构等,有时是那些库返回的内存,当我试图释放这个内存时,函数是免费的,为什么有时候我不必释放这些记忆?




您需要阅读库附带的文档。分配内存的

函数应该具有清除函数

进行解除分配或指定如何释放对象




Robert Gamble


Rodrigo Dominguez在13/06/05写道:

有有时我使用第三方库,我使用一些函数
返回char *或结构等,有时候这些库返回的内存,当我试图释放这个内存时,函数是免费的,
它打破了我的应用程序,有时它没关系,为什么?我怎么知道什么时候


不要这样做。阅读手册并按照说明操作。

我必须释放由第三方库分配的内存和
为什么有时我不必释放这些内存?



有几种方法可以实现一个函数,返回指向

对象的指针。只有图书馆文档告诉你使用这些函数的正确方法。


我个人倾向于为返回的函数添加一个''_dyn''后缀

要释放的东西让它自我记录...


-

Emmanuel

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

C库: http://www.dinkumware.com/refxc.html


有10种类型的人今天的世界;

那些了解二元的人,以及那些不懂的人。


我个人倾向于为函数添加一个''_dyn''后缀
要释放的东西以使其自我记录...




一个好的练习(我想,我又不是大师):函数从不

返回其已分配的块。这意味着调用者

永远不会释放内存,并且被调用者在函数返回之前总是释放所有分配的块。如果被叫方需要

来返回一个块,那么调用者会分配它并将其传递给

作为参数。也许有些情况下它不起作用?


there are sometimes that I use third party libraries, I use some functions
that returns char * or structs, etc. sometimes the memory that is returned
by those libraries, when I try to free this memory whith the function free,
it brokes my application, and sometimes it''s ok, why? how do I realize when
I have to free the memory that is allocated by third party libraries and
why sometimes I don''t have to free this memory?

Thank you

解决方案

Rodrigo Dominguez wrote:

there are sometimes that I use third party libraries, I use some functions
that returns char * or structs, etc. sometimes the memory that is returned
by those libraries, when I try to free this memory whith the function free,
it brokes my application, and sometimes it''s ok, why? how do I realize when
I have to free the memory that is allocated by third party libraries and
why sometimes I don''t have to free this memory?



You need to read the documentation that came with the libraries. The
functions that allocate memory should either have cleanup functions
that do deallocation or specify how you should free the objects
yourself.

Robert Gamble


Rodrigo Dominguez wrote on 13/06/05 :

there are sometimes that I use third party libraries, I use some functions
that returns char * or structs, etc. sometimes the memory that is returned
by those libraries, when I try to free this memory whith the function free,
it brokes my application, and sometimes it''s ok, why? how do I realize when
Don''t do that. Read the manual and follow the instructions.
I have to free the memory that is allocated by third party libraries and
why sometimes I don''t have to free this memory?



There are several ways to implement a function returning a pointer to
an object. Only the library documentation informs you about the proper
way of using the functions.

I personally tend to add a ''_dyn'' suffix to functions returning
something to be freed to make it self-documenting...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


I personally tend to add a ''_dyn'' suffix to functions returning
something to be freed to make it self-documenting...



A good practice (I think, I''m not a guru): a function never
returns a block it has allocated. This means that the caller
never frees memory, and the callee always frees all its
allocated blocks before function returns. If the callee needs
to return a block, then the caller allocates it and pass it
as an argument. Maybe there are situations where it doesn''t work ?


这篇关于内存分配和释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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