Hey, I need a function that virtually increase the size of an array, by creating a new array with the size of the old array plus the default array size and copying the content of the old array to the new array, finally returning the new array. This is a function that receives an old array, the size of the old array, and the default size.

so what I pictured is something like this..

int * ResizeArray(int array[], int size, int SIZE){
int * array2;
array2 = new int ;

for(int i = 0; i < size + SIZE; i++)
array2 = array;

return array2;
}

Well.. I'm still new at this C++ programing stuff and am not sure about anything..

Any hint or little help would be appreciated. thanks :$

Recommended Answers

All 2 Replies

It looks ok to me. Just make sure the calling function deletes the array when done with it to avoid memory leaks.

It looks good conceptually, but I'm a little concerned about the structure of that for loop. It overruns the boundaries of the original array.

I think you should probably use the first for loop to copy the data, then use a second for loop to finish initializing the rest of the new (larger) array. Either that or put an if in there that says if (i >=size) you store zero (0) instead of copying from the (now overrun) original array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.