Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ does implicit copy constructor copy array member variable? [duplicate]

People also ask

Are array members deeply copied in C?

C and C++ don't have "deep copy" and "shallow copy" specifically. Instead they have member-wise copy. You could call this "shallow" if the member is a pointer or resource handle, and "deep" if the member is an array or a resource handler which has a copy-constructor that duplicates the resource.

What is implicit copy constructor?

An implicitly defined copy constructor will copy the bases and members of an object in the same order that a constructor would initialize the bases and members of the object.

What are the disadvantages of copy constructor?

The only disadvantage I can think of to a copy constructor is that some large objects can be expensive to copy (eg. copying a long string involves allocating a large block of memory then copying all the content).

What is copied in copy constructor?

Copy Constructor in C++ Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.


Yes and yes is the answer. This is also true of structs in C.

typedef struct {
    int a[100];
} S;

S s1;
s1.a[0] = 42;
S s2;
s2 = s1;    // array copied

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.


"implicit copy constructor(generated by compiler)" - does a shallow copy for all variables.