英文:
Address of unique_ptr doesn't change even after std::move
问题
I am exploring unique_ptr . I am getting wrong answer than expected , Can anyone help me to find what exactly went wrong here in code .
I am using WSL(windows system for linux) on windows machine .
#include <iostream>
#include <memory>
using namespace std;
int main()
{
auto revenu_ptr{make_unique<double>(1234.64)};
cout << "Revenue ptr---------" << endl;
cout << "Original " << &revenu_ptr << " " << *revenu_ptr << endl;
auto revenue_ptr_move = std::move(revenu_ptr);
cout << "After Move " << &revenu_ptr << endl;
cout << "Revenue_ptr_move---------" << endl;
cout << "Moved Adderess " << &revenue_ptr_move << endl;
cout << "Moved Value " << *revenue_ptr_move << endl;
}
Expected Output :
Revenue ptr---------
Original 0x7ffd442e9618 1234.64
After Move 0x0
Revenue_ptr_move---------
Moved Adderess 0x7ffd442e9618
Moved Value 1234.64
But I am getting this wrong output , after move operation adderss should become zero but it is pointing at same memory location . and new adderess is there in revenue_ptr_move
Revenue ptr---------
Original 0x7ffd442e9618 1234.64
After Move 0x7ffd442e9618
Revenue_ptr_move---------
Moved Adderess 0x7ffd442e9620
Moved Value 1234.64
<details>
<summary>英文:</summary>
I am exploring unique_ptr . I am getting wrong answer than expected , Can anyone help me to find what exactly went wrong here in code .
I am using WSL(windows system for linux) on windows machine .
#include <iostream>
#include <memory>
using namespace std;
int main()
{
auto revenu_ptr{make_unique<double>(1234.64)};
cout << "Revenue ptr---------" << endl;
cout << "Original " << &revenu_ptr << " " << *revenu_ptr<< endl;
auto revenue_ptr_move = std::move(revenu_ptr);
cout << "After Move " << &revenu_ptr << endl;
cout << "Revenue_ptr_move---------" << endl;
cout << "Moved Adderess " << &revenue_ptr_move<< endl;
cout << "Moved Value " << *revenue_ptr_move << endl;
}
Expected Output :
Revenue ptr---------
Original 0x7ffd442e9618 1234.64
After Move 0x0
Revenue_ptr_move---------
Moved Adderess 0x7ffd442e9618
Moved Value 1234.64
But I am getting this wrong output , after move operation adderss should become zero but it is pointing at same memory location . and new adderess is there in revenue_ptr_move
Revenue ptr---------
Original 0x7ffd442e9618 1234.64
After Move 0x7ffd442e9618
Revenue_ptr_move---------
Moved Adderess 0x7ffd442e9620
Moved Value 1234.64
</details>
# 答案1
**得分**: 2
`&revenu_ptr`是唯一指针对象的地址。
`*revenu_ptr`是指向的数据。
`revenu_ptr.get()`是唯一指针对象的值。
你似乎把`&revenu_ptr`和`revenu_ptr.get()`混淆了。
这并不是一个罕见的问题。指针的“指针地址”与“指针值”不同。而指针值是*一个地址*的事实使得混淆更加复杂!
此外,指针所指的值与指针本身的值也不同。
指针有一个值,是一个地址。
指针的地址是一个地址。
指针指向的值有一个地址,这个地址就是指针的值。
[&ptr] -> [ptr.get()] -> [7]
这里有一个(唯一的)指针,它指向值7。
它有一个地址——存储它本身的地方。它有一个值,是它指向的值7的地址。它指向的值是一个整数,值为7。
<details>
<summary>英文:</summary>
`&revenu_ptr` is the address of the unique ptr object.
`*revenu_ptr` is the data pointed to.
`revenu_ptr.get()` is the value of the unique ptr object.
You appear to be confusing `&revenu_ptr` with `revenu_ptr.get()`.
This isn't a rare problem. The "address of a pointer" is not the same as the "value of a pointer". And the fact that the value of a pointer is *an address* adds to the confusion!
On top of that, the value the pointer POINTS AT is different than the value the pointer HAS.
The pointer has a value that is an address.
The pointer's address is an address.
The pointer's pointed-to value has an address, which is the pointer's value.
[&ptr] -> [ptr.get()] -> [7]
here we have a (unique) ptr which is pointing at the value 7.
It has an address -- where itself is stored. It has a value, which is the address of the value 7 it points at. And it has a pointed-to value, which is an integer with the value 7.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论