Hi,

I'm trying to write a function that enlarges a dynamically allocated array, but I am having some trouble doing so. Here's my code:

void enlargeArray(int *oldArr, int arraySize) 
{
   //oldArr is a dynamically allocated array

   int *newArr = new int[arraySize*2]; 

   //copy the elements from oldArr into newArr

   delete oldArr[]; 

   oldArr = newArr; 
}

However, although newArr gets created correctly, when I examine the contents of oldArr in my main function (where I call enlargeArray from), oldArr is unchanged. Does anyone have any tips for how to modify oldArr itself? Thank you.

Its complicated, but basically you need to add a level of pointer-ness. You can only return a value by address if you assign something to a memory location (IE: *var=value;) To do that here you can do this:

void enlargeArray(int **oldArr, int arraySize) 
{
   //oldArr is a dynamically allocated array

   int *newArr = new int[arraySize*2]; 

   //copy the elements from oldArr into newArr

   delete (*oldArr)[]; 

   (*oldArr) = newArr; 
}

The pointer oldArr within your function is not the same pointer as the one you pass to it (caller-side). The value (address it stores) is the same (copied when entering the function) but the pointer itself is not the same variable. What you need to do is pass it by reference, which means that the pointer within the function and the pointer that was passed to it behave as one variable. The syntax is as follows:

void enlargeArray(int *& oldArr, int arraySize);

Notice the ampersand & symbol after the star. This symbol indicates that oldArr is a reference to a pointer to an int. At the call-site, the syntax remains unchanged, within the function too.

Another alternative is to pass it as a pointer to a pointer, as so:

void enlargeArray(int ** oldArr, int arraySize) {
  //...

  delete[] *oldArr;

  *oldArr = newArr;
};

But then, you have to do this at the call-site:

enlargeArray(&oldArr, arraySize);  // notice & here, for getting the pointer to 'oldArr'.

@mike and @labdabeta - thank you! I tried passing oldArr by reference and it works. However, calling:

delete[] oldArr

doesn't seem to delete that block of memory properly - I tested this by making a temp pointer to it in my main function before the function call and then checking its value after the function call. Any tips?

You'll note that in Mike's example, the code was delete[] *oldArr not delete[] oldArr.
Oh, never mind. I see now that you tried his suggestion using a pointer reference. Personally, these sort of things often bring out compiler bugs. Since you don't say what compiler you are using, I can't say if this may be the issue here. In any case, try his suggestion of passing a pointer to the pointer. In the calling function, this may be what you would do:

int* array = new int[arraySize];
enlargeArray(&array, arraySize);

Edited 4 Years Ago by rubberman

doesn't seem to delete that block of memory properly - I tested this by making a temp pointer to it in my main function before the function call and then checking its value after the function call. Any tips?

When you do delete, it doesn't actually "erase" the memory. It only frees the memory, meaning that the memory is simply marked as being available for future dynamic allocations of memory, which might over-write it then. There is no point in erasing the memory (whatever that means.. setting all bits to zero?). So, after you delete the memory, you can still read it and get the same value as before it was deleted, however, any time soon, that could change because that chunk of memory could be allocated to something else, and, in any case, you are not "allowed" to read or write to that chunk of memory after you deleted it (and that could cause a crash under some circumstances).

This article has been dead for over six months. Start a new discussion instead.