我想知道什麼可能導致segfaults在這個簡單的例子中使用std :: experimental :: optional和union類型。有趣的是segfault和gcc都出現了segfault,但是出現在兩個不同的地方。好奇段錯誤與歧視聯盟和可選<>
我也對從下面的日誌中看到的複製和破壞的猥褻量感到困惑,想知道是否有更好的/慣用的方法來避免這麼多顯然多餘的操作?在這種情況下,考慮到這裏的所有對象都是按值傳遞和訪問的,它是否有助於切換所有構造函數以獲取右值引用並在任何地方使用std :: move?
#include <iostream>
#include <vector>
// https://github.com/akrzemi1/Optional
#include "Optional/optional.hpp"
using std::cout;
using std::vector;
using std::experimental::optional;
struct X {
int y;
X(int y) : y(y) { cout << "X::X(int)\n"; }
X(const X& x) : y(x.y) { cout << "X::X(const X&)\n"; }
~X() noexcept { cout << "X::~X()\n"; }
};
struct A {
vector<X> x;
A(const vector<X>& x) : x(x) { cout << "A::A(const vector<X>&)\n"; }
A(const A& a) : x(a.x) { cout << "A::A(const A&)\n"; }
~A() noexcept { cout << "A::~A()\n"; }
static optional<A> get() {
cout << "A::get()\n";
return A({ X(1), X(2) });
}
};
struct M {
union { A a; };
M(A a) : a(a) {cout << "M::M(A)\n";}
M(const M &m) { a = m.a; }
~M() noexcept {
cout << "M::~M()\n";
(&a)->A::~A();
}
static optional<M> get() {
cout << "M::get()\n";
auto a = A::get();
return M(*a);
}
};
struct P {
vector<M> m;
P(const vector<M>& m) : m(m) { cout << "P::P(const vector<M>&)\n"; }
P(const P& p) : m(p.m) { cout << "P::P(const P&)\n"; }
static optional<P> get() {
cout << "P::get()\n";
auto m1 = M::get();
auto m2 = M::get();
vector<M> m;
cout << "push message 1\n";
m.push_back(*m1);
cout << "push message 2\n";
m.push_back(*m2);
return P(m);
}
};
int main() {
auto p = P::get();
cout << (*p).m[1].a.x[0].y << "\n";
}
GCC失敗是這樣的:
P::get()
M::get()
A::get()
X::X(int)
X::X(int)
X::X(const X&)
X::X(const X&)
X::X(const X&)
X::X(const X&)
A::A(const vector<X>&)
X::X(const X&)
X::X(const X&)
A::A(const A&)
A::~A()
X::~X()
X::~X()
X::~X()
X::~X()
X::~X()
X::~X()
X::X(const X&)
X::X(const X&)
A::A(const A&)
M::M(A)
X::X(const X&)
X::X(const X&)
A::A(const A&)
'./a.out' terminated by signal SIGBUS (Misaligned address error)
#0 0x0000000100003c59 in X* std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<X const*, X*>(X const*, X const*, X*)()
#1 0x000000010000364e in X* std::__copy_move_a<false, X const*, X*>(X const*, X const*, X*)()
#2 0x0000000100002f3c in __gnu_cxx::__normal_iterator<X*, std::vector<X, std::allocator<X> > > std::__copy_move_a2<false, __gnu_cxx::__normal_iterator<X const*, std::vector<X, std::allocator<X> > >, __gnu_cxx::__normal_iterator<X*, std::vector<X, std::allocator<X> > > >(__gnu_cxx::__normal_iterator<X const*, std::vector<X, std::allocator<X> > >, __gnu_cxx::__normal_iterator<X const*, std::vector<X, std::allocator<X> > >, __gnu_cxx::__normal_iterator<X*, std::vector<X, std::allocator<X> > >)()
#3 0x00000001000025f8 in __gnu_cxx::__normal_iterator<X*, std::vector<X, std::allocator<X> > > std::copy<__gnu_cxx::__normal_iterator<X const*, std::vector<X, std::allocator<X> > >, __gnu_cxx::__normal_iterator<X*, std::vector<X, std::allocator<X> > > >(__gnu_cxx::__normal_iterator<X const*, std::vector<X, std::allocator<X> > >, __gnu_cxx::__normal_iterator<X const*, std::vector<X, std::allocator<X> > >, __gnu_cxx::__normal_iterator<X*, std::vector<X, std::allocator<X> > >)()
#4 0x0000000100001d19 in std::vector<X, std::allocator<X> >::operator=(std::vector<X, std::allocator<X> > const&)()
#5 0x00000001000012ad in A::operator=(A const&)()
#6 0x00000001000012d7 in M::M(M const&)()
#7 0x0000000100001356 in std::experimental::storage_t<M>::storage_t<M>(M&&)()
#8 0x0000000100001393 in std::experimental::optional_base<M>::optional_base(M&&)()
#9 0x00000001000013c4 in std::experimental::optional<M>::optional(M&&)()
#10 0x0000000100001456 in M::get()()
#11 0x00000001000016a8 in P::get()()
#12 0x0000000100000db1 in main()
而鏗鏘有時不會崩潰,有時確實像這樣:
P::get()
M::get()
A::get()
X::X(int)
X::X(int)
X::X(const X&)
X::X(const X&)
X::X(const X&)
X::X(const X&)
A::A(const vector<X>&)
X::X(const X&)
X::X(const X&)
A::A(const A&)
A::~A()
X::~X()
X::~X()
X::~X()
X::~X()
X::~X()
X::~X()
X::X(const X&)
X::X(const X&)
A::A(const A&)
M::M(A)
X::X(const X&)
X::X(const X&)
A::A(const A&)
X::X(const X&)
X::X(const X&)
M::~M()
A::~A()
X::~X()
X::~X()
A::~A()
X::~X()
X::~X()
A::~A()
X::~X()
X::~X()
M::get()
A::get()
X::X(int)
X::X(int)
X::X(const X&)
X::X(const X&)
X::X(const X&)
X::X(const X&)
A::A(const vector<X>&)
X::X(const X&)
X::X(const X&)
A::A(const A&)
A::~A()
X::~X()
X::~X()
X::~X()
X::~X()
X::~X()
X::~X()
X::X(const X&)
X::X(const X&)
A::A(const A&)
M::M(A)
X::X(const X&)
X::X(const X&)
A::A(const A&)
X::X(const X&)
X::X(const X&)
M::~M()
A::~A()
X::~X()
X::~X()
A::~A()
X::~X()
X::~X()
A::~A()
X::~X()
X::~X()
push message 1
'./a.out' terminated by signal SIGSEGV (Address boundary error)
給'A'此舉構造函數,使用'一(STD ::移動(一))',而不是拷貝構造函數'a(a)'會刪除很多這些副本 –
記錄'X'的副本和移動構造函數將有助於查看工會是否出現問題 –
@MM是的,謝謝指出,我縮短了所有變量名,所以它更易於閱讀。我已更新該帖子。 – aldanor