Is this the right way to take out the garbage?

I am looking at my code, and I am not 100% sure if I created two free store arrays or one. Here is a simplified version that shows what I don't understand.

Here is what I do in Main:

1
2
3
4
5
6
int* pMyPointer(NULL);
pMyPointer = MyClass.FunctionReturnsArrayPointer();
MyClass.FunctionThatNeedsArray(pMyPointer);

// When I am ready to take out the garbage I do it in main
delete [] pMyPointer;


Main looks OK to me but the function makes me unsure.
This is what my function looks like:

1
2
3
4
5
6
7
8
int* MyClass::FunctionReturnsArrayPointer()
{
int* pMyPointer(NULL);  // I don't have the copy from main so I declare a local one
int myArray [3] = {1,2,3};
pMyPointer = new int [3];
for (int i = 0; i < 3; i ++) pMyPointer[i] = myArray[i];
return pMyPointer;
}



I am hoping I did this right and there is only one free store array. Just want to double check that another copy wouldn't for some reason exist. So would this example make one or two free store arrays?



You are right. There is only one array created on the free store.
Thank You!
Topic archived. No new replies allowed.