Hey.
I was just wondering whether the code shown below is the most effective way of increasing the size of an array, or whether it would even work at all.
The first thing that worries me is that the return value from growArray is ignored on line 17. The next thing that I noticed is a memory leak because the return value is ignored. The newly allocated memory is still allocated with no reference to it. Meaning it is not erased. I am going to pull up an old piece of code that has an example of this type of array. In the meantime, I must ask why do you not just use vectors? http://www.cplusplus.com/reference/vector/vector/
ListArray<int>* list = new ListArray<int>();
int x = 0;
list->add(&x);
ListArrayIterator<int>* it = list->iterator();
while(it->hasNext())
{
int* num = it->next();
std::cout << *num << std::endl;
}
delete it;
delete list;
Honestly, I love vectors compared to dealing with that. That old code is something from a professor I found on the internet.
Hi, thanks for your response. I see what you mean about the return value from growArray, I should really have picked up on that. Anyway, on a separate issue, I was wondering why you would ever need to return a pointer as I understand that one of their key benefits is being able to reference the original variable rather than copying it.
After briefly reading into Vectors they look like a nice alternative, I'll have to give them a try!