我使用默认构造函数,复制构造函数,赋值运算符和析构函数声明了一个简单的结构。但是,该结构不能用作std :: map的值类型。
以下是代码:
#include <string.h>
#include <iostream>
#include <string>
#include <map>
class Foo;
std::ostream & operator<<(std::ostream & os, const Foo & v);
typedef unsigned char BYTE;
struct Foo {
char type_; // char to label type
size_t num_; // number of elem, useful if array
size_t total_; // total memory
BYTE * data_; // content of memory
Foo(const char * t) : type_('c'), num_(strlen(t)+1), total_(strlen(t)+1), data_(NULL) {
data_ = new BYTE[total_];
memcpy(data_, t, total_-1);
((char *)data_)[total_-1] = '\0';
}
Foo() : type_(), num_(), total_(), data_(NULL) {}
Foo(const Foo& rhs) : type_(rhs.type_), num_(rhs.num_), total_(rhs.total_), data_(NULL) {
if (total_) {
data_ = new BYTE[total_];
memcpy((char *)data_, (const char *)&rhs.data_, total_);
}
}
Foo & operator=(const Foo& rhs) {
if (&rhs != this) {
releaseData();
type_ = rhs.type_;
num_ = rhs.num_;
total_ = rhs.total_;
data_ = new BYTE[total_];
memcpy(data_, &rhs.data_, total_);
}
return *this;
}
~Foo() {
releaseData();
}
private:
void releaseData() {
delete [] data_; data_ = NULL;
}
};
inline std::ostream & operator<<(std::ostream & os, const Foo & v) {
os << "(type: " << v.type_ << ", num: " << v.num_ << ", total: " << v.total_ << ", data: " << (const char *)v.data_ << ", data addr: " << (void *)v.data_ << ")";
return os;
}
int main() {
Foo c("/home/data/");
std::map<std::string, Foo> store;
store["abc"] = Foo("/home/data/");
std::cout << c << std::endl;
std::cout << store["abc"] << std::endl;
}
使用gcc 4.9.2在Linux上编译代码。第一个打印正确打印出字符串,但第二个没有。
这段代码有什么问题?
答案 0 :(得分:3)
您在复制构造函数和赋值运算符中对memcpy()
的调用都是错误的。您在两种情况下都指定&rhs.data_
作为来源:
memcpy((char *)data_, (const char *)&rhs.data_, total_);
...
memcpy(data_, &rhs.data_, total_);
使用'&amp;'以这种方式,您正在复制紧跟在内存中data_
成员之后的随机字节, NOT data_
指向的字节。
由于data_
已经是指向要复制的数据的指针,因此您需要删除&
并按原样使用rhs.data_
(并且不需要类型 - 铸):
memcpy(data_, rhs.data_, total_);
或者,摆脱所有这些手动逻辑,只需使用std::string
或std::vector
,让编译器和STL为您处理所有内存管理和数据复制:
struct Foo {
char type_; // char to label type
std::string data_; // content of memory
Foo(const char * t) : type_('c'), data_(t) {}
Foo() : type_() {}
};
inline std::ostream & operator<<(std::ostream & os, const Foo & v) {
os << "(type: " << v.type_ << ", num: " << v.data_.length() << ", total: " << v.data_.capacity() << ", data: " << v.data_.c_str() << ", data addr: " << (void *)v.data_.data() << ")";
return os;
}
struct Foo {
char type_; // char to label type
std::vector<BYTE> data_; // content of memory
Foo(const char * t) : type_('c') { std::copy(t, t+(strlen(t)+1), std::back_inserter(data_)); }
Foo() : type_() {}
};
inline std::ostream & operator<<(std::ostream & os, const Foo & v) {
os << "(type: " << v.type_ << ", num: " << v.data_.size() << ", total: " << v.data_.capacity() << ", data: " << (const char*) &v.data_[0] << ", data addr: " << (void *)&v.data_[0] << ")";
return os;
}