I have read the documentation about initializing unique pointer here . I tried to declare the unique pointer the same way (see unique_ptr<int> temp1 {&h}
,I did not see this type of declaration in the docs though,just experimenting) i declare a non-smart pointer . The idea behind carrying out this experiment was to see how the std::unique_ptr::get()
method works. Here is the code :
#include<iostream>
#include<stdio.h>
#include<memory>
using namespace std ;
int main(){
int h {100};
unique_ptr<int> temp1 {&h};
cout<<"temp1.get() :"<<temp1.get()<<endl;
cout<< "&h : "<<&h<<endl;
cout<<"*temp : "<<*temp1<<endl;
return 0 ;
}
The code compiles and i get the following output :
temp1.get() :0x7ffd4322c5cc
&h : 0x7ffd4322c5cc
*temp : 100
/home/abhishek/.codelite/tmp/abhishek/codelite-exec.sh: line 3: 7889 Segmentation fault (core dumped) ${command}
Hit any key to continue...
I can see the std::unique_ptr::get()
returns the address of the managed object , which is same as &h
.
What is the error saying here ?
Although assigning address to the smart pointer has been discussed here . It does not answer my question.