gpt4 book ai didi

c++ - 获取未删除指针的值

转载 作者:太空狗 更新时间:2023-10-29 20:05:13 26 4
gpt4 key购买 nike

我正在学习 OOP,我遇到了这个问题:

如果我们这样做:

A* a = new A;

new 运算符从堆中为变量a 找到一个空间。我想知道该变量所在的地址。

问题一

那个地址是哪个?这两者有什么区别?

cout <<  a;
cout << &a;

问题2(主体)

假设我 删除 指针。程序退出。只要指针没有被类的析构函数破坏,当我再次运行程序时,我可以使用它的地址(例如 0x0035fa24)取回该对象吗?如果是,如何?

最佳答案

Question 1

Which one is that address? What is the difference between this two?

a对象 的地址。 &a指针的地址。

Question 2 (the main)

Let's assume I do NOT delete the pointer. The program exits. As long as the pointer hasn't been destructed by the class's destructor, can I get back it using it's address (e.g. 0x0035fa24), when I run the program again? If yes, how?

典型的现代操作系统不允许您这样做。当第一个进程退出时,它将回收内存。不允许后续启动的进程查看第一个进程内存的内容,因为这会带来重大的安全风险。

关于c++ - 获取未删除指针的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14716119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com