返回信息流请教达人解释下:
(1)下面程序没有发现内存泄露,请问其中的"==2885== Mismatched free() / delete / delete "是什么意思?
是说编译器自己发现new [] 和 delete mismatched? 然后自己进行了new[] 和delete []匹配?
[root@hotter test]# cat mem1.c
#include <iostream>
using namespace std;
int main()
{
int * a = new int[10];
delete a;
return 0;
}
[root@hotter test]# g++ -o mem1 mem1.c
[root@hotter test]# valgrind --tool=memcheck ./mem1
==2885== Memcheck, a memory error detector.
==2885== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==2885== Using LibVEX rev 1658, a library for dynamic binary translation.
==2885== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==2885== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==2885== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==2885== For more details, rerun with: -v
==2885==
==2885== Mismatched free() / delete / delete []
==2885== at 0x4004D31: operator delete(void*) (vg_replace_malloc.c:244)
==2885== by 0x80485F0: main (in /root/test/mem1)
==2885== Address 0x401F028 is 0 bytes inside a block of size 40 alloc'd
==2885== at 0x4005835: operator new[](unsigned) (vg_replace_malloc.c:195)
==2885== by 0x80485E2: main (in /root/test/mem1)
==2885==
==2885== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 15 from 1)
==2885== malloc/free: in use at exit: 0 bytes in 0 blocks.
==2885== malloc/free: 1 allocs, 1 frees, 40 bytes allocated.
==2885== For counts of detected errors, rerun with: -v
==2885== All heap blocks were freed -- no leaks are possible.
(2)但是下面这个程序为什么结果很不一样呢,结果泄露了124个bytes
10*12+4 = 124(传说中的标识分配空间的4个字节也被泄露了?)
[root@hotter test]# cat mem3.c
#pragma /* Edited by zhaotong */
#include <iostream>
using std::cout;
using std::endl;
class A
{
public:
A():n(2){cout<<"Call default constructor of A"<<endl;}
explicit A(int k):n(2){i = k; cout<<"Call constructor of A: one parameter"<<endl;}
inline virtual void fun1(){cout<<"fun1 of A: void"<<endl;}
void fun2(int t ){cout<<"fun2 of A: int"<<endl;}
void fun2(int t) const {cout<<"fun2 of A: const"<<endl;}
virtual ~A(){cout<<"Call destructor of A"<<endl;}
protected: int i;
private:
static int k;
const static int m = 2;
const int n;
};
int A::k = 0;
int main()
{
cout<<"sizeof A = "<<sizeof(A)<<endl;
A * p = new A[10];
delete p;
return 0;
}
[root@hotter test]# g++ -o mem3 mem3.c
[root@hotter test]# valgrind --tool=memcheck ./mem3
==3005== Memcheck, a memory error detector.
==3005== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==3005== Using LibVEX rev 1658, a library for dynamic binary translation.
==3005== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==3005== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==3005== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==3005== For more details, rerun with: -v
==3005==
sizeof A = 12
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call default constructor of A
Call destructor of A
==3005== Invalid free() / delete / delete[]
==3005== at 0x4004D31: operator delete(void*) (vg_replace_malloc.c:244)
==3005== by 0x80489DD: A::~A() (in /root/test/mem3)
==3005== by 0x8048919: main (in /root/test/mem3)
==3005== Address 0x401F02C is 4 bytes inside a block of size 124 alloc'd
==3005== at 0x4005835: operator new[](unsigned) (vg_replace_malloc.c:195)
==3005== by 0x8048847: main (in /root/test/mem3)
==3005==
==3005== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 15 from 1)
==3005== malloc/free: in use at exit: 124 bytes in 1 blocks.
==3005== malloc/free: 1 allocs, 1 frees, 124 bytes allocated.
==3005== For counts of detected errors, rerun with: -v
==3005== searching for pointers to 1 not-freed blocks.
==3005== checked 88,380 bytes.
==3005==
==3005== LEAK SUMMARY:
==3005== definitely lost: 124 bytes in 1 blocks.
==3005== possibly lost: 0 bytes in 0 blocks.
==3005== still reachable: 0 bytes in 0 blocks.
==3005== suppressed: 0 bytes in 0 blocks.
==3005== Use --leak-check=full to see details of leaked memory.
(3) 大牛们能真正权威解释下上面class A中的
inline virtual void fun1(){cout<<"fun1 of A: void"<<endl;}
在多态时的内联展开和虚函数指针到底冲突是怎么解决的吗?
这是一条镜像帖。来源:北邮人论坛 / cpp / #7648同步于 2008/5/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
请教内存释放,怕大牛们看不到,令发一帖吧
zhaotong
2008/5/28镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
inline virtual void fun1(){cout<<"fun1 of A: void"<<endl;}
在多态时的内联展开和虚函数指针到底冲突是怎么解决的吗?
---------------------------------------------------------------------------------------------------
其实inline不是在任何情况下都是有效的,就是说,有些情况下,即便你用inline修饰,编译器也不会直接展开。比如在这种情况下,应该不会展开的,如果展开的话,很显然,不可能实现多态。所以,大胆推断,这时候inline失效,虚函数还是以需函数表进行访问
其实打印信息已经说得很清楚了
在你的第2段程序中,A的构造函数被调用了10次 而析构函数只调用了一次 所以会产生泄露
第一段程序使用的是基本类型没有析构函数,所以就“凑巧没泄露”了。因为在c++标准里面这样做的后果是不可预期的,我想的确是编译器做了些优化或者转换吧
本机vs2005 对整形数组的 delete 和 delete[] 其反汇编代码都是一样的
但是valgrind 显示泄露是124字节的原因不是很清楚,你可以多测试几次以及弄清楚valgrind是如何判定泄露内存的大小的
个人愚见
【 在 zhaotong (hotter) 的大作中提到: 】
: 请教达人解释下:
: (1)下面程序没有发现内存泄露,请问其中的"==2885== Mismatched free() / delete / delete "是什么意思?
: 是说编译器自己发现new [] 和 delete mismatched? 然后自己进行了new[] 和delete []匹配?
: ...................
谢谢!
【 在 zzuwarning 的大作中提到: 】
: 其实打印信息已经说得很清楚了
: 在你的第2段程序中,A的构造函数被调用了10次 而析构函数只调用了一次 所以会产生泄露
: 第一段程序使用的是基本类型没有析构函数,所以就“凑巧没泄露”了。因为在c++标准里面这样做的后果是不可预期的,我想的确是编译器做了些优化或者转换吧
: ...................
【 在 zzuwarning 的大作中提到: 】
: 其实打印信息已经说得很清楚了
: 在你的第2段程序中,A的构造函数被调用了10次 而析构函数只调用了一次 所以会产生泄露
: 第一段程序使用的是基本类型没有析构函数,所以就“凑巧没泄露”了。因为在c++标准里面这样做的后果是不可预期的,我想的确是编译器做了些优化或者转换吧
: ...................
不可预期。
避免这种代码,有些编译器会直接拒绝。