首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

delete指针的时候可能产生的有关问题 must see:)

2012-02-04 
delete指针的时候可能产生的问题 must see:)//DeleteObject.hclassItemclassDeleteObject{public:DeleteO

delete指针的时候可能产生的问题 must see:)
//DeleteObject.h

class   Item;

class   DeleteObject
{
public:
DeleteObject(void);
public:
~DeleteObject(void);

void   DeleteItem(Item*   p);
};

//DeleteObject.cpp

#include   "StdAfx.h "
#include   "DeleteObject.h "
#include   <boost/checked_delete.hpp>

DeleteObject::DeleteObject(void)
{
}

DeleteObject::~DeleteObject(void)
{
}

void   DeleteObject::DeleteItem(   Item*   p   )
{
delete   p;
}

//item.h
#include   <iostream>

class   Item
{
public:
~Item()
{
std::cout < < "Item   destruction   ";
}
};
//main.cpp

#include   "DeleteObject.h "
#include   "Item.h "

DeleteObject   del;
del.DeleteItem(new   Item);

/////////////////////////////////

结果Item的析构函数没有被调用~   看看这是因为什么   :)


请参考http://www.devdiv.net/topic.asp?whichpage=1&TOPIC_ID=428&#842

[解决办法]
你什么编译器?VC6还是VC2003?
换VC2005/devcpp试一下就知道了。
[解决办法]
//DeleteObject.cpp

#include "StdAfx.h "
#include "DeleteObject.h "
#include <boost/checked_delete.hpp>
//////////////////////////////////////////////////////
#include "Item.h " // 加上再试..
////////////////////////////////////////////////////
DeleteObject::DeleteObject(void)
{
}

DeleteObject::~DeleteObject(void)
{
}

void DeleteObject::DeleteItem( Item* p )
{
delete p;
}

[解决办法]
楼主啊,实践出真知。
[解决办法]
DeleteObject.cpp中没有Item的完整声明,当然不知道如何释放Item对象。

怀疑能否编译通过。

[解决办法]
把class Item
{
public:
~Item()
{
std::cout < < "Item destruction ";
}
};放在class DeleteObject前试试
[解决办法]
谁让世间还存在VC6和VC2003这2个早该淘汰的东西呢。
[解决办法]
哇哈哈DeleteObject和系统函数重名了。。。改名后OK
[解决办法]
VC6 允许通过编译,但是有警告信息
这里VC6却是有些不足
[解决办法]
vc2005express:
“1> d:\tmp\t\t\t.cpp(41) : warning C4150: deletion of pointer to incomplete type 'Item '; no destructor called”
我不知道你怎么编译的。

[解决办法]
发个言
[解决办法]
广告男
[解决办法]
x.cpp: In member function `void DeleteObject::DeleteItem(Item*) ':
x.cpp:20: warning: possible problem detected in invocation of delete operator:
x.cpp:19: warning: `p ' has incomplete type
x.cpp:3: warning: forward declaration of `struct Item '


x.cpp:20: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
[解决办法]
BS楼主, 好好看上面的贴, 已经有答案了,
敢问楼主是来问问题的吗 ??
[解决办法]
这是一个error级的warning,实在没啥需要讨论的。
想共享知识就直接给结论,不用搞得自己很神神秘秘的。
[解决办法]
刚才看了楼主的链接。

“亮骚贴” 鉴定完毕..... -_-b....
[解决办法]
:) "亮骚贴”在这里是中性词 ..
[解决办法]
en,将Item的定义放到前面就解决了
[解决办法]
问题一点都不严重,因为编译器已经警告你了:warning C4150: 删除指向不完整“Item”类型的指针;没有调用析构函数

DeleteObject.cpp文件里include item.h就ok了
[解决办法]
ding
[解决办法]
mark

[解决办法]
原因是你在最开始声明class Item;时编译器并不知道Item有析构函数,
所以在调用del.DeleteItem(new Item);时只会删除该指针,不会调用该指针的Item的析构函数

如果你想输出析构函数,必须把Item的声明写在DeleteObject之前

[解决办法]
同意的代码说明如下:

//说明:定义 del 函数的时候,abc 的析构函数未定义,因此不会调用。
#include "stdafx.h "
#include <stdio.h>

class abc;

void del(abc *pobj)
{
//when call delete pob, ~abc() will not be called
//the reason is we define del() before we define class abc, we only use "class abc " delcare before del(), then del() does not know abc::~abc()
//if we put del() code after class abc, then ~abc() will be called
delete pobj;
}


class abc
{
public:
abc(){
printf( "abc\r\n ");
}


virtual ~abc(){
printf( "~abc\r\n ");
}
};


int _tmain(int argc, _TCHAR* argv[])
{
abc *pobj = new abc;
del(pobj);

//if call delete pobj, then ~abc() will be called
//delete pobj;

return 0;
}

[解决办法]
垃圾
[解决办法]
没有Item的完整声明,编译器知道怎么干吗?
好像不能吧
[解决办法]
看了下
[解决办法]
这个问题表面上的答案很简单,一目了然。

不过还是有些内在的问题不是很明白:
编译器到底从class abc;declaration语句中获取了多少信息?仅仅是只知道有这个类而已吗?指针类型不就是告诉编译器如何解释某个特定地址中内存的布局和大小吗?如果是这样,delete pobj; 语句如何将pobj所指的那一块内存的整个对象删除掉,因为此刻的编译器好像并不知道class abc的具体信息,就像它此刻并不知道abc::~abc() 一样。既然不清楚class abc的信息,如何完整删除pobj指针?
[color=#000080][/color][size=16px][/size]
[解决办法]
5 If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial
destructor or a deallocation function, the behavior is undefined.

Seams that compiler could never gather enough information to generate a error.
[解决办法]
13楼的回答不是很清楚了么,编译器已经告诉你这里不会调用destructor了,而是将这个class默认为不带destructor的指针,只执行内存释放的操作。如果有人想深究为什么这里只是一个warning而不是一个error,我看这可能是一个C语言语法兼容的后遗症。


[解决办法]
delete一个对象的时候,是需要知道这个对象的析构函数以唤醒这个析构函数,而不是需要知道这个对象占地空间多少,来释放内存,operator delete/delete[]的参数并不包含长度信息。这里编译器显然将这个类型作为不带有自定义析构函数的类型处理,并不需要知道类的具体信息。
[解决办法]
呵,之前主要是对delete的一些动作不是很明白
呵,谢Leon8086
[解决办法]
学到点东西!
delete看来还是有些东西不知道.
[解决办法]
2个超强的分类信息网站!2站在手,万事不求
----------------------------------------
http://www.allss.com.cn
----------------------------------------
齐搜网-分类信息网-面面俱到的分类信息网
个人租房|票务|兼职|跳蚤市场|二手车|生活黄页|
----------------------------------------
http://www.city2city.com.cn
----------------------------------------
城市之间-分类信息网
成千上万的租房,车辆和跳蚤市场。火车票换票 兼职交友 等信息
[解决办法]
你这个在C++标准中属于未定义行为,这种东西拿出来和++i+++i+++i一样,本身就是错误的程序,你能指望编译器给出什么合理的答案?
If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined.
[解决办法]
24楼是正解.
[解决办法]
只是前向声明了Item,没有定义,所以找不到析构函数。

[解决办法]
我严重怀疑这个程序能否编译通过
[解决办法]
我强烈过来占楼
[解决办法]
难得还有人用着落后的,界面极其龌龊的vc6,2008虽然有点慢,但是我的最爱!
[解决办法]
如果是内置类就没问题了啊
只需要用MFC对我足够了,所以我用VC6 ----我的最爱!!!!





[解决办法]
又来广告小论坛了
呵呵

[解决办法]
VC2005就慢的不行了,不知道那个VC2008要有多慢!

热点排行