![]() |
dynamically resize arrays in C++
HI.
I would like to resize the array '*data' dynamically. In C it is possible to use realloc, but what would I do in C++? Thanks. F.R. Code:
#include <iostream> |
Re: dynamically resize arrays in C++
Use vector container. It will resize automatically.
|
Re: dynamically resize arrays in C++
Hi Aztec.
thanks for the reply, but vector container takes too much time, because my class cTest contains a lot of data and the vector function push_back is too time consuming. Any other idea? |
Re: dynamically resize arrays in C++
How did you find out the time? Did you profiled your code? If yes, then show me the exact code which you profiled with the profiled output.
|
Re: dynamically resize arrays in C++
1.) How did you find out the time? --> use GetTickCount() in windows.h.
2.) then show me the exact code which you profiled with the profiled output --> see the code below. main.cpp Code:
#include <iostream>Code:
#include <iostream> |
Re: dynamically resize arrays in C++
The time penalty for vectors is usually not severe enough to warrant not using it. However, if you'd like to reallocate, simply create a new, larger dynamic array (using 'new'), copy the old contents over, and delete [] the original. Copying is obviously a time penalty, but that's precisely what realloc does in C. Try to determine the size you need in advance, or allocate as much as you can each time. If you need to upsize fairly frequently you might look into the various algorithms that are used for such operations.
|
| All times are GMT +5.5. The time now is 18:09. |