Using pointers in the example below gave me a confusion so I probably misunderstand something. I will try to show my way of understanding and some pseudo-simple example.
Creating std::unique_ptr
variable of name my_int
ensures that it can not be copied and there is only one owner of it's int
object. Then I create a vector to storage the pointer and put it without copying with std::emplace_back()
.
Now I would like to check if the element of vector integers[0]
has the same address in memory as the orginal element my_int
. I thought it should because this element could not be copied.
Example code:
auto my_int = std::make_unique<int>(1);
std::vector<std::unique_ptr<int>> integers;
integers.emplace_back(my_int.get());
std::cout<< "Value of my_int: " << *my_int << std::endl;
std::cout << "Address of my_int: " << my_int.get() << std::endl;
std::cout << "Also address of my_int: " << &my_int << std::endl;
std::cout << "Also address of my_int: " << &integers[0]<< std::endl;
std::cout << "Also address of my_int: " << integers[0].get()<< std::endl;
Result of test:
Value of my_int: 1
Address of my_int: 0x260fc40
Also address of my_int: 0x65fb58
Also address of my_int: 0x260fe20
Also address of my_int: 0x260fc40
I also tried using std::move()
when putting the object to vector but the effect is the same, addresses differs but I don't understand why.
My first confusion is that &my_int
and my_int.get()
is not the same and the second confusion is that also integers[0].get()
differs from &integers[0]
.