-1

From "Programming: Principles and Practices using C++ - Bjarne Stroustrup" example on copying arrays:

18.3.1 Copy constructors

class vector {
    int sz; // the size
    double* elem; // a pointer to the elements
public:
    vector(int s) :
            sz { s }, elem { new double[s] } {
        for (int i = 0; i < sz; ++i)
            elem[i] = 0.0; // initialize
    } // constructor
    ~vector() {
        delete[] elem;
    } // destructor
    int size() const {
        return sz;
    } // the current size
    double get(int n) const {
        return elem[n];
    } // access: read
    void set(int n, double v) {
        elem[n] = v;
    } // access: write
    vector(const vector& arg)
// allocate elements, then initialize them by copying
    :
            sz { arg.sz }, elem { new double[arg.sz] } {
        copy(arg.elem, arg.elem + arg.sz, elem); // std::copy(); see §B.5.2
    }
};
int main(int argc, char ** argv) {
    vector v(3); // define a vector of 3 elements
    v.set(2, 2.2); // set v[2] to 2.2
    vector v2 = v;
    v.set(1, 99); // set v[1] to 99
    v2.set(0, 88); // set v2[0] to
    cout << v.get(0) << ' ' << v2.get(1);
    return 0;
}

Why are the actual array members copied and not just their addresses? elem is a pointer and is not de-referenced in the copy command.

Nour
  • 75
  • 5
  • Only the size, which is a value which will be the same for the original and the copy, is copied. An entirely new array is created for the elements. –  Apr 06 '18 at 17:32
  • 2
    It is dereferenced inside `copy`. – Matteo Italia Apr 06 '18 at 17:32
  • 2
    What do you think [`std::copy`](http://en.cppreference.com/w/cpp/algorithm/copy) does? – HolyBlackCat Apr 06 '18 at 17:33
  • 1
    *Why are the actual array members copied and not just their addresses?* Follow [The Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – R Sahu Apr 06 '18 at 17:34

2 Answers2

3

Your confusion stems from the distinction between value types and reference types.

In standard C++ practices, everything is a value type unless specified otherwise. Copies of objects are fully distinct unless specifically make to be otherwise.

This is why altering a copy of a vector should not alter the original vector it was copied from regardless of how the internal workings of the vector function.

The fact that the vector is implemented using dynamically allocated memory internally is an implementation detail that has no bearing on the interface of the vector itself, which presents itself as a value type (by default)

2

To answer the question about why the array members are copied, and not just their addresses. It might be helpful to explain the difference between a shallow copy and a deep copy. Let's say we have two pointers to two different arrays, A and B.

A = [0, 1, 2, 3, 4] 
B = [9, 10, 11, 12, 13] 

Lets copy the contents of A to B. A shallow copy would involve just changing pointers. B = A

This is not always ideal, because any element you change in A is also changed in B, because they point to the same location in memory.

If you perform a deep copy, you copy all elements over from A into B.

A = [0, 1, 2, 3, 4] 
B = [0, 1, 2, 3, 4]

If you change an item in A, it will not affect the items in B. i.e changing

A[0] = 10

then,

A = [10, 1, 2, 3, 4] 
B = [0, 1, 2, 3, 4]

I hope that helped

Carson Harmon
  • 319
  • 1
  • 8