s*******1 发帖数: 20 | 1 What happens after delete? It seems that the object still exists after
delete in the program below.
Thanks.
#include
using namespace std;
class C{
public:
void f(){cout << "exist!" << endl;}
int x;
};
int main()
{
C* Cptr = new C;
Cptr->x=1;
delete Cptr;
Cptr->f();
cout << Cptr->x << endl;
return 0;
}
//output
exist!
1 |
N***m 发帖数: 4460 | 2 this is a classical example of undetermined behavior.
【在 s*******1 的大作中提到】 : What happens after delete? It seems that the object still exists after : delete in the program below. : Thanks. : #include : using namespace std; : class C{ : public: : void f(){cout << "exist!" << endl;} : int x; : };
|
h****8 发帖数: 599 | 3 delete实际上只是修改了memory pool里面的bookkeeping信息,比如修改了剩余空间开
始的地址 并没有对地址中的内容做任何变化
【在 s*******1 的大作中提到】 : What happens after delete? It seems that the object still exists after : delete in the program below. : Thanks. : #include : using namespace std; : class C{ : public: : void f(){cout << "exist!" << endl;} : int x; : };
|
z****e 发帖数: 2024 | 4 delete Cptr;
Cptr=0;
to dereference a deleted pointer is undefined. (dangerous)
to dereference a null pointer is a run time error.(safer, but bad, usually segmentation fault)
"delete" calls destructor and modify the linked list which holds address of accessible memory blocks. |
y******w 发帖数: 2220 | 5 delete只是一个逻辑删除吧。估计就像删文件一样,去掉索引项就行了,用不着把内容
去改写一遍的。 |
c******f 发帖数: 2144 | |
U********d 发帖数: 577 | 7 Delete一般是操作系统实现的,通常情况下你delete掉一个对象,操作系统只是将这个
对象所在的内存块标记为空闲,比如windows2000通常情况是把这个堆链回到freelist
或者其他几个堆管理的队列里面去。
把这块内存清0的工作是很费时间的,操作系统一般不主动作这件事情,所以删除掉后
无论内容还是对象的指针都没有任何变化。至于栈上的数值,除非你显示地指定Cptr=
null,否则在它整个生命周期里面也没有任何人会管他,这两个条件导致你短时间内无
论取成员变量还是调用成员函数都没有任何区别。
有可能变化的情况是你再次new了一个,操作系统有可能把同一块地址再分配给你然后
你又做了其他的事情。还有一种可能就是等足够长的时间System Idle Process把这块
地址清0了。
把操作系统看作政府的话,可以简单的认为delete只是贴了一个拆迁的标签,表示这栋
房子以后可以随便拆了。贴标签后你再去访问里面的人,可能钉子户还在,因为没有人
赶他走,但是你不能保证每次去都能看到,因为指不定哪天操作系统就给你拆迁了。 |
N***m 发帖数: 4460 | 8 if you set cptr=null, you may still use it in some cases.
freelist
【在 U********d 的大作中提到】 : Delete一般是操作系统实现的,通常情况下你delete掉一个对象,操作系统只是将这个 : 对象所在的内存块标记为空闲,比如windows2000通常情况是把这个堆链回到freelist : 或者其他几个堆管理的队列里面去。 : 把这块内存清0的工作是很费时间的,操作系统一般不主动作这件事情,所以删除掉后 : 无论内容还是对象的指针都没有任何变化。至于栈上的数值,除非你显示地指定Cptr= : null,否则在它整个生命周期里面也没有任何人会管他,这两个条件导致你短时间内无 : 论取成员变量还是调用成员函数都没有任何区别。 : 有可能变化的情况是你再次new了一个,操作系统有可能把同一块地址再分配给你然后 : 你又做了其他的事情。还有一种可能就是等足够长的时间System Idle Process把这块 : 地址清0了。
|
U********d 发帖数: 577 | 9 你没有得到他。
你设置了cptr=null,那么你通过cptr调用所有的成员函数和成员变量必然会导致0内存
访问错误。如果你说的some cases是之前有其他的指针比如xxoo=cptr,我觉得可以不
用拿出来说,因为使用xxoo和让cptr=null没有半点关系了。
【在 N***m 的大作中提到】 : if you set cptr=null, you may still use it in some cases. : : freelist
|
t****t 发帖数: 6806 | 10 delete is usually not implemented by OS. it's usually implemented by libc++.
freelist
【在 U********d 的大作中提到】 : Delete一般是操作系统实现的,通常情况下你delete掉一个对象,操作系统只是将这个 : 对象所在的内存块标记为空闲,比如windows2000通常情况是把这个堆链回到freelist : 或者其他几个堆管理的队列里面去。 : 把这块内存清0的工作是很费时间的,操作系统一般不主动作这件事情,所以删除掉后 : 无论内容还是对象的指针都没有任何变化。至于栈上的数值,除非你显示地指定Cptr= : null,否则在它整个生命周期里面也没有任何人会管他,这两个条件导致你短时间内无 : 论取成员变量还是调用成员函数都没有任何区别。 : 有可能变化的情况是你再次new了一个,操作系统有可能把同一块地址再分配给你然后 : 你又做了其他的事情。还有一种可能就是等足够长的时间System Idle Process把这块 : 地址清0了。
|
|
|
U********d 发帖数: 577 | 11 delete ->...-> free ->...RtlHeapXXXXX
管理堆的脏活是操作系统而不是什么libxxx干的。
+.
【在 t****t 的大作中提到】 : delete is usually not implemented by OS. it's usually implemented by libc++. : : freelist
|
t****t 发帖数: 6806 | 12 if you look at the function name: Rtl****
RTL means runtime library. obviously that's not a part of OS. i suppose that
's on windows. libc++ is exactly the runtime library.
【在 U********d 的大作中提到】 : delete ->...-> free ->...RtlHeapXXXXX : 管理堆的脏活是操作系统而不是什么libxxx干的。 : : +.
|
U********d 发帖数: 577 | 13 确实拿windows作的比方,你可以看看rtl开头的函数都是哪个dll导出的。
我觉得你可以不回答别人,但是你不能误导别人。
that
【在 t****t 的大作中提到】 : if you look at the function name: Rtl**** : RTL means runtime library. obviously that's not a part of OS. i suppose that : 's on windows. libc++ is exactly the runtime library.
|
U********d 发帖数: 577 | 14 你要这样抬杠就太没有水平了。kernel32.dll和msvcrt.dll之类的都不是OS一部分?莫
非你编译C/C++的,都自带一个内存管理的dll?laugh。
?
【在 t****t 的大作中提到】 : if you look at the function name: Rtl**** : RTL means runtime library. obviously that's not a part of OS. i suppose that : 's on windows. libc++ is exactly the runtime library.
|
t****t 发帖数: 6806 | 15 wait, you said msvcrt.dll is a part of OS?
as far as i am concerned, if there is context switching, it's a part of os k
ernel call. if there is not, it is not.
why don't you say IE is a part of OS, thus calling IE api is also OS call?
【在 U********d 的大作中提到】 : 你要这样抬杠就太没有水平了。kernel32.dll和msvcrt.dll之类的都不是OS一部分?莫 : 非你编译C/C++的,都自带一个内存管理的dll?laugh。 : : ?
|
w***n 发帖数: 1084 | 16 楼主你知道文件删掉以后只要没被覆盖也是可以复原的吗?
【在 s*******1 的大作中提到】 : What happens after delete? It seems that the object still exists after : delete in the program below. : Thanks. : #include : using namespace std; : class C{ : public: : void f(){cout << "exist!" << endl;} : int x; : };
|
N***m 发帖数: 4460 | 17 try set cptr=null in your code to see if you can still
use cptr->f()?
in my case, i can still access it.
【在 U********d 的大作中提到】 : 你没有得到他。 : 你设置了cptr=null,那么你通过cptr调用所有的成员函数和成员变量必然会导致0内存 : 访问错误。如果你说的some cases是之前有其他的指针比如xxoo=cptr,我觉得可以不 : 用拿出来说,因为使用xxoo和让cptr=null没有半点关系了。
|
m*****e 发帖数: 4193 | 18
Your last sentence is the only correct one in your post. So follow your own
advice.
【在 U********d 的大作中提到】 : 确实拿windows作的比方,你可以看看rtl开头的函数都是哪个dll导出的。 : 我觉得你可以不回答别人,但是你不能误导别人。 : : that
|
U********d 发帖数: 577 | 19 你是对的,这里我写错了。设置cptr=null后调用f()中没有查什么虚函数表之类,函数
内部也没有成员变量调用,这个情况确实可以访问。
多谢指出。:)
【在 N***m 的大作中提到】 : try set cptr=null in your code to see if you can still : use cptr->f()? : in my case, i can still access it.
|
t****t 发帖数: 6806 | 20 I also suggest you read one of my previous post:
http://www.mitbbs.com/article/Programming/31186111_0.html
It seems a lot of people confuse OS (kernel) and RTL (such as libc/libc++).
They are related but different. As I said, old fashioned people like me
consider a function call as kernel call, if there is privilege/context
switching; otherwise it is not. A lot of function are mixed, meaning they
sometimes invoke kernel, sometimes not, in other words, they *aggregate*
calls to kernel. OS librari
【在 U********d 的大作中提到】 : 确实拿windows作的比方,你可以看看rtl开头的函数都是哪个dll导出的。 : 我觉得你可以不回答别人,但是你不能误导别人。 : : that
|
|
|
z****e 发帖数: 2024 | |
U********d 发帖数: 577 | 22 看了半天我明白你的意思了,你觉得要跑在ring0的才算OS吧?我看你在OS后面加了个
括号注明了kernel。你有一个前提条件os==kernel,然后因为很明显的一个原因:
"OS libraries such as user32.dll, kernel32.dll, ntdll.dll are NOT kernels"
所以
user32.dll, kernel32.dll, ntdll.dll 不算OS。所以我说kernel32什么的算OS的一部
分是错误的。
好吧,这个推导过程没有问题。老实说我也第一次听说OS==kernel,既然你坚持这个前
提,我觉得我们再扯下去就是鸡同鸭讲了。
.
【在 t****t 的大作中提到】 : I also suggest you read one of my previous post: : http://www.mitbbs.com/article/Programming/31186111_0.html : It seems a lot of people confuse OS (kernel) and RTL (such as libc/libc++). : They are related but different. As I said, old fashioned people like me : consider a function call as kernel call, if there is privilege/context : switching; otherwise it is not. A lot of function are mixed, meaning they : sometimes invoke kernel, sometimes not, in other words, they *aggregate* : calls to kernel. OS librari
|
U********d 发帖数: 577 | 23 其实就是在说堆内存管理的问题 :)
不过讨论到后面我们纠缠于“内存管理到底是不是操作系统的一部分”,钻牛角尖的。
不涉及到新分配内存,堆回收和融合,freelist、lookaside或者heap cookie什么的。
没有什么营养,就俩小孩在斗气呢,呵呵~
【在 z****e 的大作中提到】 : 太高深了,发现我根本没有讨论资格了:──(
|
U***y 发帖数: 149 | 24 I am a newbie....
I run your code. but
the final output is
exist!
0
not the same as yours:
exist!
1
I changed Cptr->x=1000 or any other number
it always outputs like
exist!
0
Can anyone help explain?
Thansk...
【在 s*******1 的大作中提到】 : What happens after delete? It seems that the object still exists after : delete in the program below. : Thanks. : #include : using namespace std; : class C{ : public: : void f(){cout << "exist!" << endl;} : int x; : };
|
t****t 发帖数: 6806 | 25 so if i used cygwin on windows to allocate memory, i am not using OS?
【在 U********d 的大作中提到】 : 其实就是在说堆内存管理的问题 :) : 不过讨论到后面我们纠缠于“内存管理到底是不是操作系统的一部分”,钻牛角尖的。 : 不涉及到新分配内存,堆回收和融合,freelist、lookaside或者heap cookie什么的。 : 没有什么营养,就俩小孩在斗气呢,呵呵~
|
o**o 发帖数: 3964 | 26 msvcrt.dll这样的东西一般应该不算OS,因为说到底是一个方言的支持库。不过
windows比较特殊,如果删掉msvcrt.dll(我没试过)windows就不能运行了,那还是当
他是OS的一部分算了。thrust强调kernel,估计是试过了。 |