update array size dynamically

Hy guys ,

We can update our vector array if new element is added .
We are do int dynamic arrays ? in which we do't know arrray size .
and if any element is added in array then it automatically locate memory for it and update the size of array .

Is it possible ?
Sounds like you want to use std::vector.
If you're using e.g. int* parray = new int[array_size];, then the only way to resize is to alloc a new, bigger array, copy the old values to the new array, and then delete the original one.

Note that you can cut down on the number of news and deletes by tracking the elements allocated and used separately, and resize the array in blocks.

The only way to make the resize "automatic" is to write a class which wraps the array. Which is just what std::vector is.

Andy

PS Outside of an exercise, C++ coders would use std::vector, like Peter87 wrote. Note that vector is doing the allocate/copy/free for you, using a block-wise strategy.
Last edited on
Topic archived. No new replies allowed.