So I have this array of pointers
image* [] myArray;
And I copy the objects in the array into a new, larger array.
for (int i=0; i<maxObjects; i++){
newArray[i] = myImages[i];
}
for (int i=maxObjects; i<newMaxObjects; i++){
newArray[i] = NULL;
}
The point of this is to resize my array. Then I delete myArray:
delete [] myArray;
Which, I presume deletes the objects from the array, as well as the pointer to those objects. Now I want to declare myArray again
image* [] myArray;
and set this to point to my new, larger array.
myArray = newArray;
This is where I get lost: now I’ve got two pointers to the same array. myArray points to the same thing as newArray, right? Or, am i wrong, and myArray now points to newArray (the pointer), which points to the object I want?
My main questions: How do I delete the temporary pointer myArray without deleting the data it points to? Also, how do I assign the myArray pointer directly to the data, rather than pointing to another pointer? Am I doing it right or is there a better way to do what I’m doing?
got two pointers to the same array.
myArray points to the same thing as
newArray, right? Or, am i wrong, and
myArray now points to newArray (the
pointer), which points to the object
I want?
myArray is assigned the same value as newArray, which is the memory location of the first element of newArray. After you say
myArray = newArray;, if you assign newArray to NULL, myArray will still point to the datapointers.the temporary pointer myArray without deleting the data it points to?
myArray = NULL;Will just reset the pointer.delete [] myArray;will deallocate the memory you saved for the pointers that point to your data, but WILL NOT delete the actual data.pointer directly to the data,
rather than pointing to another
pointer? Am I doing it right or is
there a better way to do what I’m
doing?-
Using the indirection operator (*) on a pointer which retrieves the data it points to.
newerArraywill contain an array of pointers to data.newestArrayis an array of the data.