没有合适的资源?快使用搜索试试~ 我知道了~
首页dmalloc:检测C/C++内存泄露的高效工具
dmalloc:检测C/C++内存泄露的高效工具
需积分: 19 11 下载量 138 浏览量
更新于2024-08-02
收藏 420KB PDF 举报
dmalloc.pdf 是一份关于 DebugMallocLibrary 的文档,版本为 5.5.2,发布日期为 2007 年 5 月。该库由 Gray Watson 开发,专注于解决 C/C++ 程序中的内存管理问题,特别是内存泄露检测。它提供了一种替代系统内建的 malloc、realloc、calloc 和 free 等内存分配与释放函数,以便在开发过程中实时监控内存使用情况。 dmalloc 功能强大,它能帮助开发者追踪程序运行时的内存分配行为,确保所有动态分配的内存都被正确释放,从而避免内存泄露。通过集成到编译器或链接器中,dmalloc 可以在程序运行时自动检测内存泄漏,并且能精确地指出内存泄漏发生的位置,具体到源代码中的某一行。这对于调试复杂的应用程序尤其有用,因为它可以定位到问题的根源,提高代码质量并节省调试时间。 文档详细介绍了库的使用方法,以及版权许可信息。允许用户在保留原始版权声明和许可通知的前提下,复制和分发这份手册的原文本,同时也允许对手册进行修改并在满足特定条件(包括保留“Copying”章节的完整内容)下进行传播。对于翻译版本,同样要求保持版权和许可条件,并且如果需要,可以将“Copying”章节替换为作者批准的翻译版本。 dmalloc 是一款强大的开发者工具,对于任何注重内存管理效率和代码健壮性的项目来说,都是不可或缺的一部分。它不仅有助于提高代码质量和软件的稳定性,还能显著提升开发者的调试效率。通过理解和应用 dmalloc,程序员可以在编写 C/C++ 应用时更加自信,减少内存泄露带来的潜在问题。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/1643899/bga.jpg)
8 Dmalloc Tutorial
• Have the library check itself every 100 iterations (-i 100). This controls how fast
your program will run. Larger numbers check the heap less and so it will run
faster. Lower numbers will be more likely to catch memory problems.
• Enable a number of debug features (low). You can also try runtime for minimal
checking or medium or high for more extensive heap verification.
• By default, the low, medium, and high values enable the error-abort token which
will cause the library to abort and usually dump core immediately upon seeing an
error. See
Section 3.4 [Dumping Core], page 16. You can disable this feature
by entering dmalloc -m error-abort (-m for minus) to remove the error-abort
token and your program will just log errors and continue.
dmalloc --usage will provide verbose usage info for the dmalloc program. See Chap-
ter 4 [Dmalloc Program], page 35.
You may also want to install the ‘dmallocrc’ file in your home directory as
‘.dmallocrc’. This allows you to add your own combination of debug tokens. See
Section 4.5 [RC File], page 44.
NOTE : The output from the dmalloc utility should be captured by your shell. If you
see a bunch of stuff which includes the string DMALLOC_OPTIONS then the alias you
should have created above is not working and he environmental variables are not being
set. Make sure you’ve logged out and back in to have the alias take effect.
9. Run your program, examine the logfile that should have been created by dmalloc_
shutdown, and use its information to help debug your program.
2.3 Basic Description of Terms and Functions
2.3.1 General Memory Terms and Concepts
Any program can be divided into 2 logical parts: text and data. Text is the actual program
code in machine-readable format and data is the information that the text operates on when
it is executing. The data, in turn, can be divided into 3 logical parts according to where it
is stored: static, stack, and heap.
Static data is the information whose storage space is compiled into the program.
/* global variables are allocated as static data */
int numbers[10];
main()
{
...
}
Stack data is data allocated at runtime to hold information used inside of functions.
This data is managed by the system in the space called stack space.
void foo()
{
/* this local variable is stored on the stack */
float total;
...
![](https://csdnimg.cn/release/download_crawler_static/1643899/bgb.jpg)
Chapter 2: Description of Features and How to Get Started 9
}
main()
{
foo();
}
Heap data is also allocated at runtime and provides a programmer with dynamic mem ory
capabilities.
main()
{
/* the address is stored on the stack */
char * string;
...
/*
* Allocate a string of 10 bytes on the heap. Store the
* address in string which is on the stack.
*/
string = (char *)malloc(10);
...
/* de-allocate the heap memory now that we’re done with it */
(void)free(string);
...
}
It is the heap data that is managed by this library.
Although the above is an example of how to use the malloc and free commands, it is not
a good example of why using the heap for runtime storage is useful.
Consider this: You write a program that reads a file into memory, processes it, and
displays results. You would like to handle files with arbitrary size (from 10 bytes to 1.2
megabytes and more). One problem, however, is that the entire file must be in memory at
one time to do the calculations. You don’t want to have to allocate 1.2 megabytes when
you might only be reading in a 10 byte file because it is wasteful of system resources. Also,
you are worried that your program might have to handle files of m ore than 1.2 megabytes.
A solution: first check out the file’s size and then, using the heap-allocation routines,
get enough s torage to read the entire file into memory. The program will only be using the
system resources necessary for the job and you will be guaranteed that your program can
handle any sized file.
2.3.2 Functionality Supported by All Malloc Libraries
All malloc libraries support 4 basic memory allocation commands. These include malloc,
calloc, realloc, and free. For more information about their capabilities, check your system’s
manual pages – in unix, do a man 3 malloc.
[Function]void *malloc ( unsigned int size )
Usage: pnt = (type *)malloc(size)
![](https://csdnimg.cn/release/download_crawler_static/1643899/bgc.jpg)
10 Dmalloc Tutorial
The mallo c routine is the basic memory allocation routine. It allocates an area of
size bytes. It will return a pointer to the space requested.
[Function]void *calloc ( unsigned int number, unsigned intsize )
Usage: pnt = (type *)calloc(number, size)
The calloc routine allocates a certain number of items, each of size bytes, and returns
a pointer to the space. It is appropriate to pass in a sizeof(type) value as the size
argument.
Also, calloc nulls the space that it returns, assuring that the memory is all zeros.
[Function]void *realloc ( void *old_pnt, unsigned int new_size )
Usage: new_pnt = (type *)realloc(old_pnt, new_size)
The realloc function expands or shrinks the memory allocation in old_pnt to new_
size number of bytes. Realloc copies as much of the information from old_pnt as it
can into the new_pnt space it returns, up to new_size bytes. If there is a problem
allocating this memory, 0L will be returned.
If the old_pnt is 0L then realloc will do the equivalent of a malloc(new_size). If
new_size is 0 and old_pnt is not 0L, then it will do the equivalent of free(old_pnt)
and will return 0L.
[Function]void free ( void *pnt )
Usage: free(pnt)
The free routine releases allocation in pnt which was returned by malloc, calloc, or
realloc back to the heap. This allows other parts of the program to re-use memory
that is not needed anymore. It guarantees that the process does not grow too big and
swallow a large portion of the system resources.
WARNING: there is a quite com mon myth that all of the space that is returned by
malloc libraries has already been cleared. Only the calloc routine will zero the memory
space it returns.
2.4 General Features of the Library
The debugging features that are available in this debug malloc library can be divided into
a couple basic classifications:
file and line number information
One of the nice things about a good debugger is its ability to provide the file
and line number of an offending piece of code. This library attempts to give
this functionality with the help of cpp, the C preprocessor. See Section 3.1
[Allocation Macros], page 15
.
return-address information
To debug calls to the library from external sources (i.e. those files that could
not use the allocation macros), some facilities have been provided to supply the
caller’s address. This address, with the help of a debugger, can help you locate
the source of a problem. See Section 3.2 [Return Address], page 15.
剩余59页未读,继续阅读
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://profile-avatar.csdnimg.cn/c1acba3646f8446fab95a2c1ed029353_tianlan12345.jpg!1)
tianlan12345
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 解决本地连接丢失无法上网的问题
- BIOS报警声音解析:故障原因与解决方法
- 广义均值移动跟踪算法在视频目标跟踪中的应用研究
- C++Builder快捷键大全:高效编程的秘密武器
- 网页制作入门:常用代码详解
- TX2440A开发板网络远程监控系统移植教程:易搭建与通用解决方案
- WebLogic10虚拟内存配置详解与优化技巧
- C#网络编程深度解析:Socket基础与应用
- 掌握Struts1:Java MVC轻量级框架详解
- 20个必备CSS代码段提升Web开发效率
- CSS样式大全:字体、文本、列表样式详解
- Proteus元件库大全:从基础到高级组件
- 74HC08芯片:高速CMOS四输入与门详细资料
- C#获取当前路径的多种方法详解
- 修复MySQL乱码问题:设置字符集为GB2312
- C语言的诞生与演进:从汇编到系统编程的革命
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)