This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
g++ STL container element deletion.
- To: gcc-help at gcc dot gnu dot org
- Subject: g++ STL container element deletion.
- From: Colin Thomas <colin at designresources dot co dot uk>
- Date: Wed, 4 Oct 2000 14:19:25 +0100 (BST)
- Cc: colin at designresources dot co dot uk
- Reply-To: Colin Thomas <colin at designresources dot co dot uk>
Hi,
I have a question pertaining to STL containers.
I create a number of string pointers, and add them to
a vector.
At some later date I wish to delete ALL of the pointers
and want to use the container to do this.
When I observe memory usage through UNIX "top" though, the
memory is not being freed.
The C++ code looks like:
/////////////////////////////////////////////////////////
#include <string>
#include <vector>
#include <utility>
#include <exception>
#include <cstdio>
#include <ctime>
#include <algorithm>
using namespace std;
///////////////////////////////////
int main()
{
unsigned int i;
char str[256];
vector<string *> thisStringVector ;
vector<string *>::iterator thisStringVectorElement ;
/////////////////////////
//Create 1000000 string pointers and add to the vector
//
for (i = 0 ; i < 1000000 ; i++)
{
//Create a new string pointer...
string * pThisString;
try {
pThisString = new string ;
}
catch (...) {
cout << "Error in pThisString allocation...\n";
}
(*pThisString) = "A";
thisStringVector.push_back(pThisString);
}
/////////////////////////
//Delete 1000000 string pointers
//
for( thisStringVectorElement = thisStringVector.begin();
thisStringVectorElement != thisStringVector.end();
thisStringVectorElement++
)
{
//Delete the string contents
(**thisStringVectorElement).erase(
(**thisStringVectorElement).begin() ,
(**thisStringVectorElement).end()
);
//Delete the pointer to the strinf & free-up memory
delete (*thisStringVectorElement);
}
}
/////////////////////////////////////////////////////////
I am running GNUCC2.95.2.
I have discussed this with some other developers (VC++) and
they say this code should delete the string pointers, and free
up memory.
If there an implementaion issue with g++ STL containers OR are
my VC++ associated incorrect in their beliefs.
I lok forward to your combined wisdom.
Many thanks in advance..
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Colin Thomas.
| Design Consultant.
|
| Email : colin@designresources.co.uk
| Tele : (44) 1234 240562
| Fax : (44) 1234 241262
|
| Address : Design Resources,
| 27 Market Place,
| Olney,
| Buckinghamshire,
| United Kingdom.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~