//addStuff()
void addStuff(Blah* array[], int &nrOfItems, int &size, string such)
{
if(need to expand before adding checked by calling another function returning true or false)
{
resize(array, nrOfItems, size);
}
array[nrOfItems++] = new Blah(such);
}
//resize()
void resize(Blah* array[], int nrOfItems, int &size)
{
size += 5;
Blah **tempArray = NULL;
tempArray = new Blah*[size];
for (int i = 0; i<nrOfItems; i++)
{
string such = array[i]->getSuch();
tempArray[i] = new Blah(such);
}
for (int i = nrOfItems; i < size; i++)
{
tempArray[i] = NULL;
}
Adding up to the initial size is no problem. When eventually calling resize(), everything works fine all the way down. At the end of the expand function, we have a new array with the original name ("array"), correct data in place, more space as per the increase in size, and the array has got a new memory address (got it from tempArray). However, when we get back up to the add function, suddenly the array has got the old memory address again. I find this strange as "array" is passed to expand() by reference.
The array is then empty, so that part stuck from the resize function. Size (which is passed by reference too) also sticks with its updated value, it's only that memory address that's not sticking.
Thanks kbw, but I think that's incorrect. When passing an array to a function like this:
void thisFunction(type array[], int size)
you're effectively passing its address to the function. If you make changes to the passed array within the function, you are actually making changes to the original array used as an argument when calling the function.
As mentioned, I can alter the array in any other way (add stuff, and even deleting the contents), and those changes remain after exiting the function. The only thing that does not, is when assigning the array a new address.
This gives me the impression that the assignment takes place on the wrong level in some way. Maybe I should pass this to resize() instead:
Blah** &array (note double pointer, and here the "&" would be correct from what I can tell)
Everything works now. Will keep this thread open for a little while more though, just in case someone could explain why this actually works. :)
---
My last hunch was close, and now I've solved it. I actually need to pass "Blah** &array" both to the add function and the resize function, like this:
void addStuff(Blah** &array, int &nrOfItems, int &size, string such)
void resize(Blah** &array, int nrOfItems, int &size)
I was right in that the assignment of the memory address takes place "on another level", but at the same, I would also have thought that these two statements were equivalent:
Blah* array (one level of dereference)
Blah** &array (two levels of dereference and then a reference, giving a "net" equivalent to one level of dereference).
Apparently, this is not true, given the different outcomes.
Can somebody spell those two statements out and show wherein lies the difference between them?
Blah** &array;
array is a reference to a pointer to a pointer to a Blah object. You can use array just like a Blah** but any changes made to array will also affect the argument that was passed to the function.
Blah** &array is rather a pointer to a pointer to a referenced object, which for me would imply going one step in the other direction, like as if the last "*" and the "&" would cancel out each other.