Just to make it as clear as possible:
struct X
{
char data_[100];
};
X a, b;
a.data_[10] = 'x';
b = a;
// here, b.data_[n] == a.data_[n] for 0 <= n < 100, so b.data_[10] == 'x'
BUT, the potentially nasty case is for pointers and references:
struct X
{
char* data_[100];
};
X a, b;
a.data_[10] = new char[6]; // a character array on the heap
strcpy(a.data_[10], "hello"); // put some text into it...
b = a;
// here, b.data_[n] == a.data_[n] for 0 <= n < 100
// so b.data_[10] == a.data_[10] == same character array containing "hello"
// BUT...
b.data_[10][2] = 'L'; // change text to "heLlo" via b.data_[10] pointer...
// here, a.data_[10][2] will be 'L' too, as a.data_[10] and b.data_[10] both point
// to the same underlying heap memory returned by new above...
delete[] a.data_[10]; // ok...
std::cout << b.data_[10]; // NOT ok - this memory's been deallocated!
delete[] b.data_[10]; // NOT ok - this memory's (already) been deallocated!
Hopefully that helps illustate the issue.
Consider one way to make the structure more "copy-safe":
struct X
{
X(const X& rhs)
{
for (int i = 0; i < 100; ++i)
if (rhs.data_[i])
{
// deep copy of pointed-to text...
data_[i] = new char[strlen(rhs.data_[i]) + 1];
strcpy(data_[i], rhs.data_[i]);
}
else
data_[i] = NULL;
}
char* data_[100];
};
Here, the copy-constructor makes X b = a
safer and more intuitive because it makes its own copy of all the string data and has no further dependency on or connection to the copied X
object, but this is slower and potentially more wasteful of memory.