tags:

views:

130

answers:

4

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?

+2  A: 

You need a new array to hold the pointers.

image* [] myArray = new image* [...number of image* elements needed..];

for (int i=0; i<newMaxObjects; i++){
  myArray[i] = newArray[i];
}

See this article on array resizing: http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html

Michael Baker
+3  A: 

You should definitely look into STL (Standard Template Library) containers. A good resource is located here: http://www.cplusplus.com/reference/stl/

You can use the vector class, for instance, for storing the image instances. It will automatically grow whenever there's a need for more space. It'd go like this:

vector<image> imageVector;
imageVector.push_back(aImageInstance1);
imageVector.push_back(aImageInstance2);

That's pretty much a c++ approach for the problem, letting the STL work out the container allocation for you.

Diego Medaglia
+1  A: 

Which, I presume deletes the objects from the array, as well as the pointer to those objects.

Incorrect; this only removes the space allocated for the array of pointers, and not the objects they point to. However, since you have moved these to a new, larger array, this isn't a problem.

Pointers and c-style arrays can be a tricky subject, and I think diagrams are one of the best ways of figuring out what's going on. You have myArray, which is really a Image**

                     myArray's array        The objects pointed to by the array.
myArray (Image**) -> myArray[0] (Image*) -> *(myArray[0]) (Image)
                  -> myArray[1] (Image*) -> *(myArray[1]) (Image)
                  -> myArray[2] (Image*) -> *(myArray[0]) (Image)

You then create a new array of pointers which point to the same objects:

                     myArray's array        The objects              newArray's array
myArray (Image**) -> myArray[0] (Image*) -> *(myArray[0]) (Image) <- newArray[0] (Image*) <- newArray (Image**)
                  -> myArray[1] (Image*) -> *(myArray[1]) (Image) <- newArray[1] (Image*)
                  -> myArray[2] (Image*) -> *(myArray[0]) (Image) <- newArray[2] (Image*)
                                            NULL                  <- newArray[3]

When you delete myArray, you only delete the array of pointers, leaving newArray pointing the underlying element:

  The objects              newArray's array
  *(myArray[0]) (Image) <- newArray[0] (Image*) <- newArray (Image**)
  *(myArray[1]) (Image) <- newArray[1] (Image*)
  *(myArray[0]) (Image) <- newArray[2] (Image*)
  NULL                  <- newArray[3]

When you do the final myArray = newArray, they are both pointing to the same array of pointers. This is fine, as long as you only delete one of them.

Todd Gardner
A: 
  • 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?

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.

  • My main questions: How do I delete 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.

  • 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?-

Using the indirection operator (*) on a pointer which retrieves the data it points to. newerArray will contain an array of pointers to data. newestArray is an array of the data.

image* newerArray = new (image*)[size];
for (int i = 0; i < size; i++)
{
    newerArray[i] = *(myArray[i]);
}



image newestArray = new image[size];
for (int i = 0; i < size; i++)
{
    newestArray[i] = *(*(myArray[i]));
}
Legatou
Hey thanks -- so as it stands, when I do myImages = newArray;do I need to set newArray to null, or does it not matter? There won't be memory leakage because it's just a pointer, right?