std::vector 如何将对象复制到其内部存储
我有以下问题:
void add(){
cRow Row();
Row.add("Column", "Value");
std::vector<cRow> mRows;
mRows.push_back(Row);
}
cRow::cRow(): mCol(NULL), mVal(NULL) {
}
cRow::add(const char* Col, const char* Val){
mCol = strdup(Col);
mVal = strdup(Val);
}
cRow::~cRow(){
free(mCol);
free(mVal);
}
将局部变量 Row 添加到向量后,为该 Row 调用析构函数并释放字符串。
显然,指向向量中存储行的字符串的指针现在也被释放了。离开本地范围后对行的任何访问都将导致段错误。
行的转储会在 2 个调用之后进行如下处理:
| (null) | (null) |
-----------------------------------------------------
| (null)| (null) |
| LastContainerUpdatePropagation| 1307967498 |
------------------------ END ------------------------
在 3 个调用之后:
| (null) | (null) |
-----------------------------------------------------
| (null)| (null) |
| (null)| (null) |
| LastSystemUpdatePropagation| 1307967498 |
------------------------ END ------------------------
在完全离开范围而不添加新行之后,每一行都被释放。
所以,现在我的问题是: std:vector 如何复制对象?我必须做什么才能保留指向字符串的指针或将它们复制到另一个空间?
非常感谢!
I have the following problem:
void add(){
cRow Row();
Row.add("Column", "Value");
std::vector<cRow> mRows;
mRows.push_back(Row);
}
cRow::cRow(): mCol(NULL), mVal(NULL) {
}
cRow::add(const char* Col, const char* Val){
mCol = strdup(Col);
mVal = strdup(Val);
}
cRow::~cRow(){
free(mCol);
free(mVal);
}
After adding the local variable Row to the vector, the destructor is called for that Row and the strings are freed.
Obviously, the pointers to the strings of the stored row in the vector are now freed as well. Any access to the rows after leaving the local scope will result in segfaults.
The dump of the rows looks after 2 calls like that:
| (null) | (null) |
-----------------------------------------------------
| (null)| (null) |
| LastContainerUpdatePropagation| 1307967498 |
------------------------ END ------------------------
after 3 calls:
| (null) | (null) |
-----------------------------------------------------
| (null)| (null) |
| (null)| (null) |
| LastSystemUpdatePropagation| 1307967498 |
------------------------ END ------------------------
and after leaving the scope completely without adding a new row, every row was freed.
So, now my question: How does std:vector copy objects? What do I have to do to keep the pointers to the strings or to copy them into another space?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::vector
使用复制构造函数来复制对象。由于您尚未定义复制构造函数,因此它将使用隐式 C++ 复制构造函数,其中只是递归复制所有成员;这还不够,因为您正在手动管理内存。您需要定义自己的复制构造函数,或者使用诸如 std::string 之类的东西,如果复制的话,它将执行正确的操作。
作为一个好的实践,任何具有重要析构函数的类都应该有一个复制构造函数和复制赋值运算符,正是这个原因(这被称为 三规则)。如果复制构造函数没有意义(例如,对于
ostream
之类的东西),则应将其设为私有,以防止意外复制。std::vector
uses the copy constructor to copy objects. Since you have not defined a copy constructor, it will use the implicit C++ copy constructor, which just copies all members recursively; this isn't enough, since you are manually managing memory.You need to either define your own copy constructor, or use something like
std::string
, which will do the right thing if copied.As a matter of good practice, any class with a non-trivial destructor should have a copy constructor and copy assignment operator, for precisely this reason (this is known as the rule of three). If a copy constructor doesn't make sense (eg, for things like
ostream
), it should be made private, in order to prevent accidental copying.std::vector
使用复制构造函数来初始化其元素。如果您没有明确编写,编译器会提供一个,但是当您进行内存管理时,您需要:cRow
类中根据需要使用尽可能多的包装器(请注意,您仍然需要为您的 RAII 容器提供适当的复制构造函数、复制赋值运算符和析构函数)。const char*
,它可以替换为std::string
,它将处理内存管理。注意:只有方法 #3 可以避免编写这三个函数的自定义版本。
std::vector
uses the copy constructor to initialise its elements. If you haven't written on explicitly, the compiler provides one, but as you are doing memory management, you need to either:cRow
class (note that you will still need to provide the appropriate copy constructor, copy assignment operator and destructor for your RAII container).const char*
, which could be replaced withstd::string
, which will handle the memory management.Note: only approach #3 avoids having to write a custom version of the three functions.