C++ 内存分析-valgrind

valgrind包括了以下几个比较重要的模块:memcheck, cachegrind, callgrind, helgrind, drd, massif, dhat, sgcheck, bbv. 还有一些小工具,但不是每个人都能用上,比如Lackey, Nulgrind.

在linux系统下下载源程序之后,通过./configure, make, make install命令安装在系统中。

用法: $valgrind  [valgrind-options]  your-prog  [your-prog-options]

The most important option is --tool which dicates which valgrind tool to run. 默认情况下是 --tool==memcheck . 

Memcheck is a memory error detector. 它能检测到C和C++程序中的下述问题:

1. 访问不该访问的内存  2. 使用未定义的变量  3. 堆内存的不正确释放,例如多次使用堆块,或者malloc/new/new[]与free/delete/delete[]的错配。

4. src和dst指针指向的内存区域存在重叠  5. passing a fishy ( possibly negative) value to the size parameter of a memory allocation function.  6. 内存泄露

 如果使用malloc, calloc, realloc, valloc或者memalign, 必须以free释放

如果使用new分配内存,必须使用delete释放

如果使用new[]分配内存,必须以delete[]释放

 

each bit in the system has an associated V bit, which follows it around everywhere, even inside the CPU. it says whether or not the accompanying bit has a legitimate value.

Copying values around dose not cause Memcheck to check for, or report on , errors. However, when a value is used in a way which might conceivably affect your program's externally-visible behaviour, the associated V bits are immediately checked. If any of these indicate that the value is undefined (even partially), an error is reported.

这段话简单来说就是,每一bit的数据,都有一个伴随bit,称为 V(valid-value) bit. 它存储了这一bit数据是否合法(是否被定义)。只是简单复制数据,memcheck并不会检查和回报对应V bit的信息。但只用使用到的数据存在破坏程序的迹象,对应的V bits就会立即被检查,如过使用到的数据不合法,memcheck便会立即回报错误。例如一个数据被用作判断条件时,他对应的V bits就会被检查。

checks on defineness only occur in three places: when a value is used to generate a memory address, when control flow decision needs to be made, and when a system call is detected. Memcheck checks definedness of parameters as required.

Every bit in memory or in the CPU has an associated valid-value( V) bit. In addition, all bytes in memory, but not in the CPU, has an associated valid-address (A) bit. This indicates whether or not the program can legitimately read or write that location.

 

posted @ 2016-05-03 04:52  雨落无声2002  阅读(256)  评论(0编辑  收藏  举报