I have a vector member and want to reallocate memory. Say I have 10 elements in a vector "vec" and I don't need them any more, so they can be erased or whatever. In the next stage I need memory just for 5 elements. In order to reallocate the vector:
1) Should I call to vec.~vector<T>() and then declare again vector<double> vec(5) ?
2) Should I use vec.resize(5)? In this case what does happen with the other 5 elements that were before? are they still allocated in memory or were they deallocated?
3) Should I use vec.reserve(5)? In this case what does happen with the other 5 elements that were before? are they still allocated in memory or were they deallocated?
Neither constructors nor destructors can be called directly. Local variables can't be deallocated without popping the stack (usually by returning from a function, but also by closing a block). Popping the stack deallocates all variables in the same scope.
std::vector::reserve() changes the physical size of the vector without changing its logical size. Reserve()ing a size larger than the current causes a reallocation, but reserve()ing one smaller has no effect.
std::vector::resize() changes the logical size of the vector. If the target size is larger than the physical size, the vector is reallocated.
num = 10;
for (i = 0; i < num-1; i++)
{
size = (i +1) * n; // where n is a random number between 1 and 10
vector<double> vec(size, 0.0);
for (j = 0 ; j < size-1; j++)
{
vec[j] = (j + 1) * (i + n);
}
// now I do something with "vec". Once I have it done I don't
// need the previously calculated "vec" anymore. I always want to use
// exactly the total capacity, so I would like to deallocate the previously
// calculated "vec" because I am going to declare again the "vec" with
// the new capacity in the next i-loop
}
I pressume that I should do something in order to not cause a failure when the declaration "vector<double> vec(size, 0.0) " for i = 1 comes into the picture.
The vector is inside a loop, so at every iteration the vector is popped and pushed back. In other words. The vector won't hold the data longer than the current iteration.
so, what should I do? I was thinking that I can use resize or clear or erase function. But these functions will not free the memory. Maybe I don't need to free them and just work with them, though... But anyway, when I was working with C-arrays I simply did the following which is what I would like to emule with the vector class:
double* vec;
num = 10;
for (i = 0; i < num-1; i++)
{
size = (i +1) * n; // where n is a random number between 1 and 10
vec = new double[size]
for (j = 0 ; j < size-1; j++)
{
vec[j] = (j + 1) * (i + n);
}
// now I do something with "vec". After this I don't need it anymore, so
// I delete it.
Do you realize that that method also loses the data in the array after each iteration? If that's what you want to do, then the vector code you have there will do.
Yes, that is what I told. I want to deallocate and allocate once again in the next iteration. In other words I want to destroy the "vec" and then create it again in th next iteration. How can I do this with the vector class? Look at the code with C-arrays. That is exactly what I want to do using the vector class. Or there is no way to do that with the vector class?
You should have used a better language. "Reallocate" is not the same as "deallocate and allocate". The code you posted earlier will do:
num = 10;
for (i = 0; i < num-1; i++)
{
size = (i +1) * n; // where n is a random number between 1 and 10
vector<double> vec(size, 0.0);
for (j = 0 ; j < size-1; j++)
{
vec[j] = (j + 1) * (i + n);
}
// now I do something with "vec". Once I have it done I don't
// need the previously calculated "vec" anymore. I always want to use
// exactly the total capacity, so I would like to deallocate the previously
// calculated "vec" because I am going to declare again the "vec" with
// the new capacity in the next i-loop
Ok. thanks. Does this mean that when declaring "vector<double> vec(size, 0.0)" in the next loop the destructor of vector class is called and destroy my previous "vec"?
Well, the destructor is not called when the loop restarts, but at the end of the loop. Specifically, at the final brace. At that point all local variables and objects that were allocated are popped from the stack. Doing this is what call the destructor of std::vector.
ok, i see. So, at each "i" I am creating another vector but the previous one is somewhere in the memory until the last "i" finishes after which the destructor is called. is that right? Is that mean if num = 10 then I will have created 10 different vectors when the loop finishes?