Reputation: 25
I declare a simple struct together with default constructor, copy constructor, assignment operator, and destructor. However, the struct does not work as a value type for std::map.
Here is the code:
#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;
}
The code compiles on Linux with gcc 4.9.2. The first print correctly print out the string, but the second does not.
What is wrong with this piece of code?
Upvotes: 0
Views: 207
Reputation: 595711
Your calls to memcpy()
in both the copy constructor and assignment operator are wrong. You are specifying &rhs.data_
as the source in both cases:
memcpy((char *)data_, (const char *)&rhs.data_, total_);
...
memcpy(data_, &rhs.data_, total_);
By using '&' in this manner, you are copying random bytes that immediately follow the data_
member in memory, NOT the bytes that data_
is pointing at.
Since data_
is already a pointer to the data being copied, you need to drop the &
and just use rhs.data_
as-is (and there is no need for the type-casts):
memcpy(data_, rhs.data_, total_);
Alternatively, get rid of all this manual logic and just use std::string
or std::vector
instead, and let the compiler and STL handle all of the memory management and data copying for you:
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;
}
Upvotes: 3