c++ delete void*
2013-11-15 15:08
309 查看
附注:在C语言中,void*型的指针与其它类型的指针直接互相赋值,不用强制转换;在C++中,从其它类型的指针到void*的赋值不用强制转换,但从void *到其它类型的指针的赋值需要强制转换。
正文:
前一段时间看代码:
RESULT SWF_Storage_FreeFile(void *pBuffer)
{
if (pBuffer) delete[] pBuffer;
return SWF_S_OK;
}
有如下几点疑问;
1.若是built-in类型,不会出现太大的问题,heap能够正常释放。
2.若是cllass对象,则会出现状况。
class Test
{
public:
Test(char flag);
~Test();
private:
void * m_data;
char m_flag;
};
Test::Test(char flag)
{
m_flag = flag;
cout<<"constructing entity "<<m_flag<<endl;
m_data = new char[100];
memset(m_data,1,100);
char* data= (char*)m_data;
cout << data << endl;
}
Test::~Test()
{
cout<<"destructing entity "<<m_flag<<endl;
delete[] m_data;
char* data= (char*)m_data;
cout << data << endl;
}
int main(int argc, char* argv[])
{
Test * a = new Test('a');
delete a;
a =NULL;
void * b = new Test('b');
delete b;
b = NULL;
return 0;
}
[root@localhost c++]#
delete b; 这条语句不会调用析构函数。造成m_dada分配的heap内存无法释放,内存泄露!
==8501==
==8501== HEAP SUMMARY:
==8501== in use at exit: 100 bytes in 1 blocks
==8501== total heap usage: 4 allocs, 3 frees, 232 bytes allocated
==8501==
==8501== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==8501== at 0x4A07062: operator new[](unsigned long) (vg_replace_malloc.c:363)
==8501== by 0x400A7B: CEntity::CEntity(char) (in /home/work/c++/test4)
==8501== by 0x400BDD: main (in /home/work/c++/test4)
==8501==
==8501== LEAK SUMMARY:
==8501== definitely lost: 100 bytes in 1 blocks
==8501== indirectly lost: 0 bytes in 0 blocks
==8501== possibly lost: 0 bytes in 0 blocks
==8501== still reachable: 0 bytes in 0 blocks
==8501== suppressed: 0 bytes in 0 blocks
==8501==
==8501== For counts of detected and suppressed errors, rerun with: -v
==8501== ERROR SUMMARY: 305 errors from 10 contexts (suppressed: 4 from 4)
正文:
前一段时间看代码:
RESULT SWF_Storage_FreeFile(void *pBuffer)
{
if (pBuffer) delete[] pBuffer;
return SWF_S_OK;
}
有如下几点疑问;
1.若是built-in类型,不会出现太大的问题,heap能够正常释放。
2.若是cllass对象,则会出现状况。
class Test
{
public:
Test(char flag);
~Test();
private:
void * m_data;
char m_flag;
};
Test::Test(char flag)
{
m_flag = flag;
cout<<"constructing entity "<<m_flag<<endl;
m_data = new char[100];
memset(m_data,1,100);
char* data= (char*)m_data;
cout << data << endl;
}
Test::~Test()
{
cout<<"destructing entity "<<m_flag<<endl;
delete[] m_data;
char* data= (char*)m_data;
cout << data << endl;
}
int main(int argc, char* argv[])
{
Test * a = new Test('a');
delete a;
a =NULL;
void * b = new Test('b');
delete b;
b = NULL;
return 0;
}
[root@localhost c++]#
delete b; 这条语句不会调用析构函数。造成m_dada分配的heap内存无法释放,内存泄露!
==8501==
==8501== HEAP SUMMARY:
==8501== in use at exit: 100 bytes in 1 blocks
==8501== total heap usage: 4 allocs, 3 frees, 232 bytes allocated
==8501==
==8501== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==8501== at 0x4A07062: operator new[](unsigned long) (vg_replace_malloc.c:363)
==8501== by 0x400A7B: CEntity::CEntity(char) (in /home/work/c++/test4)
==8501== by 0x400BDD: main (in /home/work/c++/test4)
==8501==
==8501== LEAK SUMMARY:
==8501== definitely lost: 100 bytes in 1 blocks
==8501== indirectly lost: 0 bytes in 0 blocks
==8501== possibly lost: 0 bytes in 0 blocks
==8501== still reachable: 0 bytes in 0 blocks
==8501== suppressed: 0 bytes in 0 blocks
==8501==
==8501== For counts of detected and suppressed errors, rerun with: -v
==8501== ERROR SUMMARY: 305 errors from 10 contexts (suppressed: 4 from 4)
相关文章推荐
- 【ThinkingInC++】65、使用delete void*可能会出错
- 【ThinkingInC++】65、使用delete void*可能会出错
- C++"void __cdecl operator delete(void *)"原因
- C++随笔 delete void *
- (vc6.0)C++"void __cdecl operator delete(void *)"原因
- delete void* 释放内存问题(C++)
- C/C++语言void及void指针深层探索
- C++学习笔记--重载new和delete
- C++进阶—>malloc/free与new/delete的区别
- C++中关于new和delete的使用
- c++中的delete和delete[]
- More Effective C++ 条款8 了解各种不同意义的new和delete
- c++中new和delete的使用方法
- 【C/C++】void指针知多少
- C++中delete和delete[]的区别[转]
- C/C++语言void及void指针深层探索
- C++中的内存管理(new、delete、内存泄漏)
- 原生JS:delete、in、typeof、instanceof、void详解
- C/C++【初级】void与void*详解
- C++中delete和delete[]的区别