什么删除命令真正做内存,对于C ++中的指针? [英] What does delete command really do for memory, for pointers in C++?

查看:94
本文介绍了什么删除命令真正做内存,对于C ++中的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++删除 - 它删除我的对象,但我仍然可以访问数据?

局部变量的内存是否可以在其范围之外访问?

我不明白 delete 当我想要释放分配给 new 的内存时真的做到。在C ++ Premiere书中,它被写为:

I do not understand what delete really does when I want to free memory allocated with new. In C++ Premiere book it is written:


这会删除ps指针指向的内存;它不会删除
指针本身。例如,您可以重复使用ps来指向另一个新分配。你
应该总是平衡使用新的和使用删除;否则,可以用
结束内存泄漏 - 即已分配但不能再使用的内存。如果
内存泄漏增长太大,它可以带来一个程序寻求更多的
内存停止。

This removes the memory to which ps pointer points; it doesn’t remove the pointer ps itself. You can reuse ps, for example, to point to another new allocation. You should always balance a use of new with a use of delete; otherwise, you can wind up with a memory leak—that is, memory that has been allocated but can no longer be used. If a memory leak grows too large, it can bring a program seeking more memory to a halt.

所以我理解 delete 必须删除pinter指向的内存中的值。但它不。这是我的实验:

So as I understand delete must delete the value in the memory to which pinter points. But it doesn't. Here's my experiment:

int * ipt = new int;   // create new pointer-to-int

cout << ipt << endl;   // 0x200102a0, so pointer ipt points to address 0x200102a0
cout << *ipt << endl;  // 0, so the value at that address for now is 0. Ok, nothing was assigned
*ipt = 1000;     // assign a value to that memory address
cout << *pt << endl;   // read the new value, it is 1000, ok
cout << *((int *) 0x200102a0) << endl;   // read exactly from the address, 1000 too

delete ipt;   // now I do delete and then check
cout << ipt << endl;  // 0x200102a0, so still points to 0x200102a0
cout << *ipt << endl;  // 1000, the value there is the same
cout << *((int *) 0x200102a0) << endl;    // 1000, also 1000 is the value

那么 delete 真的吗?

So what does delete really do?

推荐答案

将内存视为一个大型仓库,当你叫新,仓库工作人员发现一个未使用的盒子足够大你的需要,记录该盒子是你拥有的(所以它不是给别人),并给你的盒子的数量,所以你可以把你的东西进入它。这个数字将是指针。

Think of memory as a big warehouse with lots of boxes to put things into. When you call "new", the warehouse staff finds an unused box large enough for your needs, records that box as being owned by you (so it's not given to someone else), and gives you the number of that box so you can put your stuff into it. This number would be the "pointer".

现在,当删除该指针时,相反的情况发生:仓库工作人员注意到这个特定的框可用。与真正的仓库工作人员相反,他们没有做任何事情与盒子—所以如果你看看它后,删除,你可能会看到你的旧东西。

Now, when you "delete" that pointer, the reverse happens: the warehouse staff notes that this particular box is available again. Contrary to real warehouse staff they aren't doing anything with the box — so if you look into it after a "delete" you might see your old stuff. Or you might see somebody else’s stuff, if the box was reassigned in the meantime.

从技术上讲,你不能在你退回之后再查看你的盒子,否则你可能会看到别人的东西。到池,但这是一个有点奇怪的仓库没有钥匙或警卫,所以你仍然可以做任何你想要的。但是,这可能会导致盒子的新所有者出现问题,因此您希望您遵守这些规则。

Technically, you are not allowed to look into your box once you have returned it to the pool, but this is a somewhat weird warehouse with no keys or guards, so you can still do whatever you want. However, it might cause problems with the new owner of the box, so it's expected that you follow the rules.

这篇关于什么删除命令真正做内存,对于C ++中的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆