vector::clear() vs. new() / delete()

From: Massimiliano (sirbone72_at_yahoo.it)
Date: 04/30/04


Date: 30 Apr 2004 06:28:43 -0700

Hi,
I'm studying the behaviour of vector class and I I was amazed by the
strange (to me) use of clear(). Initially, I thought that the clear()
method released all the memory allocated by the vector, but I read
that it depends on the compiler! Am I correct?
Can it produce memory leakage?
As an example, I tryied this:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
        vector<int> vs, vs2;
        
        for(int i=0; i<10000000;i++)
        {
                vs.push_back(i);
        }
        cin.get(); //a pause to let me control memory usage

        vs.clear();
        cin.get(); // a second pause to let me control if memory is released

        for(int i=0; i<10000000;i++)
        {
                vs2.push_back(i);
        }
        cin.get(); //Other memory is allocated!
        
        return 0;
}

On my system (win2000, g++ 3.2.3) I saw that the memory allocated by
vector vs is not released. The program allocates other memory for the
second vector(vs2).
If I had used new and delete, the program would have released the
memory used by the first vector.
Could someone explain to me where's the error?
Thanks!



Relevant Pages

  • Re: vector::clear() vs. new() / delete()
    ... >I'm studying the behaviour of vector class and I I was amazed by the ... >strange use of clear. ... >method released all the memory allocated by the vector, ... The program allocates other memory for the ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Program Memory Low - Select a task to close or increase progra
    ... Why don't you run the check before your program allocates some memory? ... "Tsviatko Yovtchev" wrote: ... it would not prevent the app from ...
    (microsoft.public.dotnet.framework.compactframework)
  • VB.net Memory usage 20k app, 15MB memory?
    ... Is there anyway to reduce the amount of memory a VB.net program allocates? ... Just a simple windows form, with one button and a label, and all the button ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Maximum memory available in XP
    ... There's no simple answer since it depends on virtual memory fragmentation ... other memory your program allocates, how many threads you have (and when ... to allocate a single contiguous chunk. ...
    (microsoft.public.vc.language)
  • Memory manager behaviour on Solaris 8
    ... My question is regarding the memory manager and when the allocated memory ... In other words, if a program allocates about 10Mb then free them, I expect ... event after several hours the process memory size doesn't decrease. ...
    (comp.unix.solaris)