Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is any copying happening here?

As I understand it, you can’t return an array from a method because you are actually returning a pointer to data created in the method’s scope. There’s no guarantee that the data will still be at the address pointed to by that pointer outside the method scope.

But it seems to work for constructors because this code prints true every time I run it. Am I just getting lucky?

class Vector3d {

    public:
        float* data_ptr;

    public:
        Vector3d(float values[3]) {
            data_ptr = values;
        }
        
};

int main(){
    float arr[] = {3, 4, 5};
    Vector3d vec = Vector3d(arr);
    
    cout << (vec.data_ptr[0] == arr[0]) << endl;
}

I think my other option is to make the member variable an array instead of a pointer and use a "copy" constructor:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Vector3d {

    public:
        float data_ptr[3];

    public:
        Vector3d(float values[3]) {
            for (int i = 0; i < 3; i++) {
                data_ptr[i] = values[i];
            }
        }
        
};

Does that mean that data_ptr[i] and values[i] are now two distinct values stored at distinct memory addresses? Or are those variables sharing a memory location?

>Solution :

An array type in a function parameter (including constructor parameters) has special rules that make it behave as if it was declared with as pointer instead, for almost all purposes.

So

    Vector3d(float values[3])

is actually

    Vector3d(float* values)

So, in the first snippet, you are just storing a pointer value pointing to arr from main into data_ptr. arr in main lives until the end of main. So you can still access it. When you access data_ptr[i] you are actually accessing arr[i].

Nothing is copying an array in this code. You are only manipulating pointers. If you want arrays to have copy behavior like other types, then use std::array instead of built-in arrays. std::array behaves like any scalar or class type, while built-in arrays have weird behaviors inherited from C.


In the second snippet you are explicitly copying the elements from arr into a new array data_ptr. The data_ptr array is distinct from the arr array and since their lifetimes overlap they must be located at distinct addresses. What I said above still applies though: values is just a pointer to arr.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading