vector::clear() vs. new() / delete()
From: Massimiliano (sirbone72_at_yahoo.it)
Date: 04/30/04
- Next message: Paul: "Re: C++ Conversion functions for pointers"
- Previous message: Paul: "Re: Overloaded operator += using pointer"
- Next in thread: Leor Zolman: "Re: vector::clear() vs. new() / delete()"
- Reply: Leor Zolman: "Re: vector::clear() vs. new() / delete()"
- Reply: Francis Glassborow: "Re: vector::clear() vs. new() / delete()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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!
- Next message: Paul: "Re: C++ Conversion functions for pointers"
- Previous message: Paul: "Re: Overloaded operator += using pointer"
- Next in thread: Leor Zolman: "Re: vector::clear() vs. new() / delete()"
- Reply: Leor Zolman: "Re: vector::clear() vs. new() / delete()"
- Reply: Francis Glassborow: "Re: vector::clear() vs. new() / delete()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|