| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1008 人关注过本帖
标题:[经验]malloc之后要记得free
只看楼主 加入收藏
栖柏
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1103
专家分:17
注 册:2007-8-23
收藏
 问题点数:0 回复次数:6 
[经验]malloc之后要记得free

程序中间动态分配了内存,但是在程序结束时没有释放这部分内存,逐渐耗尽内存资源,导致系统崩溃。
malloc之后没有及时free释放内存有可能导致以上的内存泄露现象,所以要注意使用
关于c语言指针和内存泄露,我摘自别人文章的一段:
use memset along with malloc, or always use calloc.
1.永远把memset和malloc一起使用,或者永远用calloc

* Whenever writing values to pointers, make sure you cross check the number of bytes available and number of bytes being written.
2.但写数据到指针所指的内存时,一定要检查可用空间的大小,和写入的字节数。

* Before assigning the pointers, make sure no memory locations will become orphaned.
3.在给一个指针赋值的时候,确定没有内存区域丢失。

* Whenever freeing the structured element (which in turn contains the pointer to dynamically allocated memory location), first traverse to the child memory location and start freeing from there, traversing back to the parent node.
4.当释放结构体成员的内存时(这个结构体有成员指针指向动态分配的内存),要先把子内存区域释放掉,再释放父内存区域。

* Always properly handle return values of functions returning references of dynamically allocated memory.
5.适当的处理返回指向动态分配的内存的指针的函数。(好绕嘴+_+)

* Have a corresponding free to every malloc.
6.对于每一个malloc都有一个对应的free

* Make sure you are not accessing null pointer.
7.确保你不会访问null指针。

你记住了么~~~~

搜索更多相关主题的帖子: malloc 内存 free 经验 number 
2007-08-25 01:52
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
c区也能见到刷屏!

Fight  to win  or  die...
2007-08-25 03:51
百年不亮
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:789
专家分:0
注 册:2006-4-14
收藏
得分:0 

你们昨晚睡觉了没? 这么晚还在发帖子,精神让我感动。

2007-08-25 09:54
魔城侠客
Rank: 1
等 级:新手上路
帖 子:200
专家分:0
注 册:2006-4-4
收藏
得分:0 
凭着不懈的精神,我顶了

West and east,home is best……
2007-08-25 12:03
栖柏
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1103
专家分:17
注 册:2007-8-23
收藏
得分:0 
不好意思见笑了,刷屏作品啊
但觉得说的不一定别人都知道呀
有些体味吧

You have lots more to work on! Never give up!c language!
2007-08-25 14:58
windflush
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:886
专家分:0
注 册:2007-7-1
收藏
得分:0 
顶一下把,说得确实都是内存使用中大家都应知道,而且即使知道,但是在实际使用中还是会漏掉的东西。

2007-08-25 15:01
努力学编程
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-8-25
收藏
得分:0 
顶哦!
2007-08-25 15:43
快速回复:[经验]malloc之后要记得free
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019255 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved