[英]push_back crashed when using std::vector if the type only defined copy constructor with c++11
The code like this:像这样的代码:
#include <iostream>
#include <vector>
struct Foo
{
int i;
double d;
};
class Boo
{
public:
Boo() : fptr(nullptr)
{
std::cout << "Boo default construct..." << std::endl;
}
Boo(int i, double d):fptr(new Foo{i,d})
{
}
Boo(const Boo &rhs) :fptr(new Foo{rhs.fptr->i,rhs.fptr->d})
{
}
Foo *fptr;
};
int main(int argc, char const *argv[])
{
std::vector<Boo> vec(1);
Boo b(42, 10.24);
vec.push_back(b);
return 0;
}
Env: Ubuntu 16.4 LTS with gcc 5.4 g++ -std=c++11 test.cpp -o test -g环境:Ubuntu 16.4 LTS 与 gcc 5.4 g++ -std=c++11 test.cpp -o
./test 。/测试
If define move constructor,this will work good.如果定义移动构造函数,这将工作得很好。 I debuged it several hours but can not found what's wrong with it, maybe because some rules in c++ 11 that i did't know.
我调试了几个小时,但找不到它有什么问题,可能是因为我不知道 c++ 11 中的一些规则。
Could somebody help me?有人可以帮我吗? Thanks!
谢谢!
Your Boo
copy constructor derefernces a pointer fptr
that may be null.您的
Boo
复制构造函数取消引用可能是 null 的指针fptr
。
A std::vector::push_back
may reallocate vector storage. std::vector::push_back
可以重新分配向量存储。 Reallocating may use copy constructors.重新分配可以使用复制构造函数。
That is what is happening in your program.这就是您的程序中正在发生的事情。 You are copying a
Boo
that has a null pointer.您正在复制具有 null 指针的
Boo
。
The problem is that in the copy constructor of Boo
you're dereferencing a nullptr
.问题在于,在
Boo
的复制构造函数中,您取消了对nullptr
的引用。 This is because when you wrote:这是因为当你写:
std::vector<Boo> vec(1); //this creates a vector of size 1 using Boo's default constrcutor
This is what happens due to the above statement:这是由于上述声明而发生的情况:
vec
of size 1
using Boo
's default constructor .Boo
的默认构造函数创建了一个大小为1
的名为vec
的向量。1
Boo
object inside vec
.vec
内部已经有1
Boo
object 。 Moreover since this Boo
object was created using the default constructor, its fptr
is set to nullptr
.Boo
object 是使用默认构造函数创建的,因此它的fptr
设置为nullptr
。 Now, when you wrote:现在,当你写道:
vec.push_back(b);// this adds/push_back object b onto the vector vec so that now its size will be 2
Now, these are the important things that you have to remember here:现在,这些是您必须记住的重要事项:
1
Boo
object inside it which has its fptr
as nullptr
.1
Boo
object ,其fptr
为nullptr
。Boo
will be used.Boo
的复制构造函数。Boo
object, it dereferences its fptr
.Boo
object 时,它会取消引用它的fptr
。 But since that fptr
is nullptr
and we know that dereferencing a nullptr
is undefined behavior , so this may cause the program to segmentation fault as in your case.fptr
是nullptr
并且我们知道取消引用nullptr
是未定义的行为,因此这可能会导致程序出现分段错误,就像您的情况一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.