I have this C++11 code that uses unique_ptr
in vector.
#include <vector>
#include <iostream>
#include <memory>
using namespace std;
class A
{
int x;
public:
A() {}
~A() {}
A(A& a) {}
A(int x) {this->x = x;}
int get() {return x;}
};
int main()
{
vector<unique_ptr<A>> v;
auto a = new A(10);
unique_ptr<A> pa(a);
v.push_back(move(pa)); // move(pa);
auto a2 = new A(20);
unique_ptr<A> pb(a2);
v.push_back(move(pb)); // move(pa);
for (auto& i: v)
{
cout << i->get();
}
}
I could use Xcode to debug and check value in vector>. However, when I compile the same code with clang and debug in lldb, there are two errors.
The first one is lldb traces into STL source code.
* thread #1: tid = 0x1f03, 0x0000000100001744 a.out`__gnu_cxx::__normal_iterator<std::unique_ptr<A, std::default_delete<A> >*, std::vector<std::unique_ptr<A, std::default_delete<A> >, std::allocator<std::unique_ptr<A, std::default_delete<A> > > > >::operator*() const at stl_iterator.h:740, stop reason = step over
frame #0: 0x0000000100001744 a.out`__gnu_cxx::__normal_iterator<std::unique_ptr<A, std::default_delete<A> >*, std::vector<std::unique_ptr<A, std::default_delete<A> >, std::allocator<std::unique_ptr<A, std::default_delete<A> > > > >::operator*() const at stl_iterator.h:740
737
738 // Forward iterator requirements
739 reference
-> 740 operator*() const
741 { return *_M_current; }
742
743 pointer
(lldb) n
Process 41243 stopped
The second error is I have segmentation error when I tried to see the vector contents.
(lldb) p v
Segmentation fault: 11
I used this command for compilation.
clang++ -std=c++11 -stdlib=libc++ -g testit.cpp -o a
What might be wrong?