How to free memory used in a vector<float>

Discussion in 'C++' started by silverburgh.meryl@gmail.com, Jul 16, 2008.

  1. Guest

    Hi,

    I have a class attribute 'vector<float>'

    class MyClass {
    public:
    virtual ~MyClass();
    private:
    vector<float> aVector;
    };


    I would like to know what do I need to do to free the memory used in
    'aVector'.
    I think i need to do 'clear()' to free each float in the aVector, but
    do I need to anything specifically to free the vector itself.

    MyClass::~MyClass() {
    aVector.clear();
    }

    Thank you.
     
    , Jul 16, 2008
    #1
    1. Advertising

  2. Jerry Coffin Guest

    In article <0ea398d4-14b5-41dd-9409-aa681cb74c43
    @y38g2000hsy.googlegroups.com>, says...
    > Hi,
    >
    > I have a class attribute 'vector<float>'
    >
    > class MyClass {
    > public:
    > virtual ~MyClass();
    > private:
    > vector<float> aVector;
    > };
    >
    >
    > I would like to know what do I need to do to free the memory used in
    > 'aVector'.
    > I think i need to do 'clear()' to free each float in the aVector, but
    > do I need to anything specifically to free the vector itself.
    >
    > MyClass::~MyClass() {
    > aVector.clear();
    > }


    As far as destroying the vector goes, you don't have to do anything --
    the vector gets destroyed when the MyClass object is destroyed, and
    vector's dtor releases whatever memory it's using.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jul 16, 2008
    #2
    1. Advertising

  3. Markus Moll Guest

    Hi

    Alf P. Steinbach wrote:

    > Why are you using 'float' instead of 'double'?
    >
    > 'double' is likely more efficient (or just as efficient)


    That depends. E.g., Intel recommends using the smallest possible data-type
    so that MMX/SSE can be used more efficiently.

    Markus
     
    Markus Moll, Jul 16, 2008
    #3
  4. Markus Moll wrote:
    > Hi
    >
    > Alf P. Steinbach wrote:
    >
    >> Why are you using 'float' instead of 'double'?
    >>
    >> 'double' is likely more efficient (or just as efficient)

    >
    > That depends. E.g., Intel recommends using the smallest possible data-type
    > so that MMX/SSE can be used more efficiently.


    Also since 'double' takes twice as much space as 'float', some caching
    and other memory issues might also kick in in some situations, making it
    less efficient (especially if the vector is very large).
     
    Juha Nieminen, Jul 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    663
  2. Replies:
    1
    Views:
    853
    Sebastian Millies
    Nov 2, 2006
  3. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
  4. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    444
    Keith Thompson
    Dec 27, 2007
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,648
    James Kanze
    Oct 8, 2009
Loading...

Share This Page