best way to dynamically create a new array

Afternoon.

I'm working on a small project due in on Monday. I'm after creating a function that will extend an array. I don't want to use link lists or anything like that, has to be the array.

I've created my array as
1
2
int arrSize = 5; 
data = new T[arrSize];


And what I'm doing is, is that when it tries to add 1 + arrSize, it will call a function that will take the array, create a temp array, for loop through the existing array and copy the contents into the temp array. Delete the data[], create a new one with arrSize * 2 (doubling it each time). And then doing another for loop to copy it back into new enlarged array.

What I'm wondering is, is it possible to improve the efficiency of this?

Appreciate any comments and thoughts.

J
Resizing an array should not take more than one pass through the array to copy it. I'll let you figure it out, but give you a clue: Suppose you have a glass of water; how would you put the water in a bigger glass?
Topic archived. No new replies allowed.