“unique_ptr”的地址即使在std::move之后也不会改变。

huangapple go评论68阅读模式
英文:

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 &lt;&lt; &quot;After Move &quot; &lt;&lt; &amp;revenu_ptr &lt;&lt; endl;
cout &lt;&lt; &quot;Revenue_ptr_move---------&quot; &lt;&lt; endl;
cout &lt;&lt; &quot;Moved Adderess &quot; &lt;&lt; &amp;revenue_ptr_move&lt;&lt; endl;
cout &lt;&lt; &quot;Moved Value &quot; &lt;&lt; *revenue_ptr_move &lt;&lt; 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

`&amp;revenu_ptr`是唯一指针对象的地址。

`*revenu_ptr`是指向的数据。

`revenu_ptr.get()`是唯一指针对象的值。

你似乎把`&amp;revenu_ptr`和`revenu_ptr.get()`混淆了。

这并不是一个罕见的问题。指针的“指针地址”与“指针值”不同。而指针值是*一个地址*的事实使得混淆更加复杂!

此外,指针所指的值与指针本身的值也不同。

指针有一个值,是一个地址。

指针的地址是一个地址。

指针指向的值有一个地址,这个地址就是指针的值。


    [&amp;ptr] -&gt; [ptr.get()] -&gt; [7]

这里有一个(唯一的)指针,它指向值7。

它有一个地址——存储它本身的地方。它有一个值,是它指向的值7的地址。它指向的值是一个整数,值为7。

<details>
<summary>英文:</summary>

`&amp;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 `&amp;revenu_ptr` with `revenu_ptr.get()`.

This isn&#39;t a rare problem.  The &quot;address of a pointer&quot; is not the same as the &quot;value of a pointer&quot;.  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&#39;s address is an address.

The pointer&#39;s pointed-to value has an address, which is the pointer&#39;s value.


    [&amp;ptr] -&gt; [ptr.get()] -&gt; [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>



huangapple
  • 本文由 发表于 2023年5月22日 17:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定