mercredi 2 septembre 2015

why std::unique_ptr vector gets invalid pointer exception

I wrote simple code to help me understand smart pointers:

string s = "str";
vector <unique_ptr<string>> pv ;

pv.push_back(unique_ptr<string>(&s));
cout<<*(pv[0])<<endl;

This code compiles fine, but gets me a runtime error:

str * Error in `...': munmap_chunk(): invalid pointer: 0x00007ffd956e57e0 * Aborted (core dumped)

What happened and what have I done wrong?

Aucun commentaire:

Enregistrer un commentaire