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

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

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 .

  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4. int main()
  5. {
  6. auto revenu_ptr{make_unique<double>(1234.64)};
  7. cout << "Revenue ptr---------" << endl;
  8. cout << "Original " << &revenu_ptr << " " << *revenu_ptr << endl;
  9. auto revenue_ptr_move = std::move(revenu_ptr);
  10. cout << "After Move " << &revenu_ptr << endl;
  11. cout << "Revenue_ptr_move---------" << endl;
  12. cout << "Moved Adderess " << &revenue_ptr_move << endl;
  13. cout << "Moved Value " << *revenue_ptr_move << endl;
  14. }

Expected Output :
Revenue ptr---------
Original 0x7ffd442e9618 1234.64
After Move 0x0
Revenue_ptr_move---------
Moved Adderess 0x7ffd442e9618
Moved Value 1234.64

  1. 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

  1. <details>
  2. <summary>英文:</summary>
  3. 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 .
  4. 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;

  1. auto revenue_ptr_move = std::move(revenu_ptr);
  2. cout &lt;&lt; &quot;After Move &quot; &lt;&lt; &amp;revenu_ptr &lt;&lt; endl;
  3. cout &lt;&lt; &quot;Revenue_ptr_move---------&quot; &lt;&lt; endl;
  4. cout &lt;&lt; &quot;Moved Adderess &quot; &lt;&lt; &amp;revenue_ptr_move&lt;&lt; endl;
  5. 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

  1. 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

  1. </details>
  2. # 答案1
  3. **得分**: 2
  4. `&amp;revenu_ptr`是唯一指针对象的地址。
  5. `*revenu_ptr`是指向的数据。
  6. `revenu_ptr.get()`是唯一指针对象的值。
  7. 你似乎把`&amp;revenu_ptr`和`revenu_ptr.get()`混淆了。
  8. 这并不是一个罕见的问题。指针的“指针地址”与“指针值”不同。而指针值是*一个地址*的事实使得混淆更加复杂!
  9. 此外,指针所指的值与指针本身的值也不同。
  10. 指针有一个值,是一个地址。
  11. 指针的地址是一个地址。
  12. 指针指向的值有一个地址,这个地址就是指针的值。
  13. [&amp;ptr] -&gt; [ptr.get()] -&gt; [7]
  14. 这里有一个(唯一的)指针,它指向值7。
  15. 它有一个地址——存储它本身的地方。它有一个值,是它指向的值7的地址。它指向的值是一个整数,值为7。
  16. <details>
  17. <summary>英文:</summary>
  18. `&amp;revenu_ptr` is the address of the unique ptr object.
  19. `*revenu_ptr` is the data pointed to.
  20. `revenu_ptr.get()` is the value of the unique ptr object.
  21. You appear to be confusing `&amp;revenu_ptr` with `revenu_ptr.get()`.
  22. 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!
  23. On top of that, the value the pointer POINTS AT is different than the value the pointer HAS.
  24. The pointer has a value that is an address.
  25. The pointer&#39;s address is an address.
  26. The pointer&#39;s pointed-to value has an address, which is the pointer&#39;s value.
  27. [&amp;ptr] -&gt; [ptr.get()] -&gt; [7]
  28. here we have a (unique) ptr which is pointing at the value 7.
  29. 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.
  30. </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:

确定