1

Suppose in C++, I have the following code:

class Foo {
private:
    double* myData;
public:
    Foo(double data[]) {
        myData = data;
    }
}

int main() {
    double mainData[] = {1.0};
    Foo myfoo(mainData);
}

As far as my knowledge can tell, mainData is treated as a pointer when passed into the Foo constructor, so the line myData = data only assigns the pointer address. So no extra memory is allocated here, right? But then, is the Foo class responsible for providing a destructor that deallocates myData's memory? Or do we have a dynamic array pointer that actually points to stack memory?

Also, if I want to protect Foo's myData from changing when mainData is changed, is there a simple way to force the Foo constructor to copy it? Ideally myData would be a simple array, not a pointer, but changing the line double* myData to double myData[] doesn't seem to work because the size of the array is unknown until runtime.

1
  • 2
    This kind of issue leads to a lot of bugs in C++. It's very important to have clear data ownership policies in your programs. If you want Foo to own its data, replace myData with a std::vector<double> and fill it as needed. Or double myData[SIZE] if the size is fixed at compile time. Commented Apr 3, 2014 at 19:43

5 Answers 5

3

The parameter here is not a dynamic array:

Foo(double data[])

In fact the declaration is equivalent to this:

Foo(double * data)

Even decltype will tell you they are the same thing, and those two signatures will conflict as overloads.

So, there is no allocation. You are only passing a pointer to the first element of the array.

Also, the only place where C++ will automatically copy an array is when it is a member of a class, and the empty bracket [] syntax for indeterminate size is not allowed for members. (Or if it is, the size is already determined by the time the class type is complete, before the copy constructor or assignment operator is generated.)

Also, if I want to protect Foo's myData from changing when mainData is changed, is there a simple way to force the Foo constructor to copy it? Ideally myData would be a simple array, not a pointer, but changing the line double* myData to double myData[] doesn't seem to work because the size of the array is unknown until runtime.

You can keep a copy of the data, but you will need a pointer if its size (or at least an upper bound) is unknown at compile time. I would recommend std::vector over a naked pointer, or at least std::unique_ptr< double[] >.

Sign up to request clarification or add additional context in comments.

Comments

2

In this case myData points to an address on the stack, which calls the destructor for Foo when the function goes out of scope. Generally arrays are described as being dynamic when you use the keyword new to allocated them.

As for your second question, you're probably going to have to pass into the constructor a pointer to the array and the length of the array. You then need to dynamically create a double array (pointed to by myData), using the length that was passed in, and then make a copy.

Don't forget to delete the memory in the destructor.

Comments

1
  1. A pointer only holds a memory address, without new or delete involved a pointer has nothing to do with allocation or deallocation. Thus your code wont invoke any memory allocation.
  2. In order to delete an (dynamically allocated) array you have to do delete[] foo;
  3. Only dynamically allocated objects must be deleted, if you class takes ownership (it manages the array, calls delete on destruction) passing an array with automatic storage duration is a very bad idea.

Comments

0

Yes, it does not allocate additional memory.

No, the destructor won't do anything with the class field if it hadn't been told so.

Comments

0

in Foo class instances will have pointer to data that is allocated/managed by other classes this is very bad design. The best thing is to make the Foo constructor make a copy and store it in the pointer. Then in the desctructor free that one. This would require passing the length of the array to the Foo constructor. I hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.