示例:
class Person
{
public:
Person(int age)
{
//将年龄数据开辟到堆区
m_Age = new int(age);
}
//重载赋值运算符 写法2 此代码在linux测试
Person& operator=(Person& p)
{
*m_Age = *p.m_Age; //通过linux下valgrind工具检测,无内存泄漏情况。
//此语句是把this->m_age所指向的内存里的值,将20替换成18
//返回自身
return *this;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//年龄的指针
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
return 0;
}
holo@jiayinhao:~/test$ valgrind ./shenkaobei
22518 Memcheck, a memory error detector
22518 Copyright © 2002-2013, and GNU GPL’d, by Julian Seward et al.
22518 Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info== 版本号==
22518 Command: ./shenkaobei
22518 22518是进程号
p1的年龄为:18
p2的年龄为:18
p3的年龄为:18
22518
22518 HEAP SUMMARY:
22518 in use at exit: 0 bytes in 0 blocks
22518 total heap usage: 3 allocs, 3 frees, 12 bytes allocated 堆空间使用情况:申请三次,释放3次
22518
22518 All heap blocks were freed – no leaks are possible
22518
22518 For counts of detected and suppressed errors, rerun with: -v
22518 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) 错误总结:没有错误
#include <stdio.h>
#include <stdlib.h>
int main()
{
//内存泄漏
char *p1 = (char *)malloc(sizeof(char) * 128);
//越界访问
int *p2 = (int *)malloc(sizeof(int) * 5);
p2[5] = 1; // Invalid write of size 4
free(p2);
//未初始化的内存
char *p3; //野指针
char ch = *p3; // Use of uninitialised value of size 8
//使用已经释放的内存
char *p4 = (char *)malloc(sizeof(char) * 8);
free(p4);
p4[0] = 'a'; //Invalid write of size 1
return 0;
}
编译时带上-g选项,用valgrind工具执行时才会显示内存泄漏错误所在的行号
holo@jiayinhao:~/test$ gcc valgrind_test.c -o valgrind_test -g
holo@jiayinhao:~/test$ valgrind ./valgrind_test
22586 Memcheck, a memory error detector
22586 Copyright © 2002-2013, and GNU GPL’d, by Julian Seward et al.
22586 Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
22586 Command: ./valgrind_test
22586
22586 Invalid write of size 4
22586 at 0x4005A9: main (valgrind_test.c:11)
22586 Address 0x5200114 is 0 bytes after a block of size 20 alloc’d
22586 at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
22586 by 0x40059C: main (valgrind_test.c:10)
22586
22586 Use of uninitialised value of size 8
22586 at 0x4005BF: main (valgrind_test.c:16)
22586
22586 Invalid write of size 1
22586 at 0x4005E3: main (valgrind_test.c:21)
22586 Address 0x5200160 is 0 bytes inside a block of size 8 free’d
22586 at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
22586 by 0x4005DE: main (valgrind_test.c:20)
22586
22586
22586 HEAP SUMMARY: //堆内存的使用情况
22586 in use at exit: 128 bytes in 1 blocks
22586 total heap usage: 3 allocs, 2 frees, 156 bytes allocated //申请了三次释放了两次
22586
22586 LEAK SUMMARY: 内存泄露情况汇总
22586 definitely lost: 128 bytes in 1 blocks
22586 indirectly lost: 0 bytes in 0 blocks
22586 possibly lost: 0 bytes in 0 blocks
22586 still reachable: 0 bytes in 0 blocks
22586 suppressed: 0 bytes in 0 blocks
22586 Rerun with --leak-check=full to see details of leaked memory
22586
22586 For counts of detected and suppressed errors, rerun with: -v
22586 Use --track-origins=yes to see where uninitialised values come from
22586 ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0) //总结:有三个(内存泄漏除外)错误