1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

how to reallocate a STL vector

Discussion in 'C' started by aortizb, Sep 25, 2008.

  1. aortizb

    aortizb New Member

    Joined:
    Sep 12, 2008
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    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?

    4) any other suggestion

    Thanks in advance.

    Alejandro
     
  2. aortizb

    aortizb New Member

    Joined:
    Sep 12, 2008
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    I complete my problem here.

    My problem is something like this:

    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.

    so, any thought?
     
  3. aortizb

    aortizb New Member

    Joined:
    Sep 12, 2008
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    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.

    delete [] vec;

    }
     
  4. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    I would just allocate a vector that is the biggest you will need and use that,
    only freeing it at the end. If you can't do that for some reason, then use resize.
     

Share This Page