STL hash_map and free/delete the key object

A

Arun Dharankar

Greetings...

Towards the end of this post is a sample C++ code using STL
hash_map. The question is: how do I delete the keys (free the
memory used for)? When I execute this, the process size
keeps growing at each iteration.

Note that the example uses std::string as the key, but this
key could be more complex (say, a string and an int). I am
attempting this on a Linux box (Fedora core 1) with GCC 3.3.2.

Best regards,
-Arun.

/*
To compile: g++ -g -o c c.c
To execute: ./c 100000 10 1000
*/

#include <iostream>
#include <string>
#include <ext/hash_map>
#include <boost/intrusive_ptr.hpp>

struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};


int main(int ac, char *av[])
{
__gnu_cxx::hash_map<const char*, std::string *,
__gnu_cxx::hash<const char*>, eqstr> num;
char t[32];
int i, j, k, m, div, max, times;

max = atoi(av[1]);
times = atoi(av[2]);
div = atoi(av[3]);

printf("max=%d times=%d div=%d\n", max, times, div);
system("/bin/ps -l");

std::string *x;

k = 0;
for(j=0; j<times; j++) {
for(i=0; i<max; i++) {
std::string *v = new std::string();
std::string *k = new std::string();
// --- WANT TO FREE THIS ---
sprintf(t, "%06d", i);
v->assign(t);
k->assign(t);
num[k->c_str()] = v;
}

system("/bin/ps -l | grep \" c$\" | grep -v grep");

m = 0;
for(i=0; i<max; i++) {
sprintf(t, "%06d", i);
x = num[t];
num.erase(t); // --- Object deleted from the hash_map
delete x; // --- Memory for the object free'd
// --- So, HOW DO I DELETE THE MEMORY ASSOCIATED WITH THE KEY
}
}
system("/bin/ps -l | grep \" c$\" | grep -v grep");
}
 
R

Roberto =?ISO-8859-15?Q?D=EDaz?=

Arun said:
Greetings...

Towards the end of this post is a sample C++ code using STL
hash_map. The question is: how do I delete the keys (free the
memory used for)? When I execute this, the process size
keeps growing at each iteration.

I am not able to compile your code in my debian woody system.. I am lacking
the extensions.. anyway.. see below.
/*
To compile: g++ -g -o c c.c
To execute: ./c 100000 10 1000
*/

................ snip .................
for(j=0; j<times; j++) {
for(i=0; i<max; i++) {
std::string *v = new std::string();
std::string *k = new std::string();

here you are obtaining two strings using new... I dont see that you are
deleting them so far.. so it is not surprise you have a memory leak.
// --- WANT TO FREE THIS ---
sprintf(t, "%06d", i);
v->assign(t);
k->assign(t);
num[k->c_str()] = v;

here you can delete both v and k since the keys and the values will be
passed to the container by value and it will have its own private copy that
it will manage by itself..

I dont see how is that you dont delete both... maybe I am not understanding
something else you want to do.

If you want your containers to contain references instead of the objects
themselves then store pointers... not objects.

Regards

Roberto
 
A

Arun Dharankar

If I free the memory associated with the key in the first inner
"for" loop, the second "for" loop will get a SIGSEGV when accessing
the value/object in the hash_map. From this I interpret that the
hash_map is not making a copy of the key, and relies on the memory
allocated with the key at time of inserting the key/value.


Best regards,
-Arun.


Roberto Díaz said:
Arun said:
Greetings...

Towards the end of this post is a sample C++ code using STL
hash_map. The question is: how do I delete the keys (free the
memory used for)? When I execute this, the process size
keeps growing at each iteration.

I am not able to compile your code in my debian woody system.. I am lacking
the extensions.. anyway.. see below.
/*
To compile: g++ -g -o c c.c
To execute: ./c 100000 10 1000
*/

............... snip .................
for(j=0; j<times; j++) {
for(i=0; i<max; i++) {
std::string *v = new std::string();
std::string *k = new std::string();

here you are obtaining two strings using new... I dont see that you are
deleting them so far.. so it is not surprise you have a memory leak.
// --- WANT TO FREE THIS ---
sprintf(t, "%06d", i);
v->assign(t);
k->assign(t);
num[k->c_str()] = v;

here you can delete both v and k since the keys and the values will be
passed to the container by value and it will have its own private copy that
it will manage by itself..

I dont see how is that you dont delete both... maybe I am not understanding
something else you want to do.

If you want your containers to contain references instead of the objects
themselves then store pointers... not objects.

Regards

Roberto
 
A

Arun Dharankar

Thanks for your reply, Roberto.
The problem was my inaccurate understanding of the way the
memory is managed in STLs.

Best regards,
-Arun.


Roberto Díaz said:
Arun said:
Greetings...

Towards the end of this post is a sample C++ code using STL
hash_map. The question is: how do I delete the keys (free the
memory used for)? When I execute this, the process size
keeps growing at each iteration.

I am not able to compile your code in my debian woody system.. I am lacking
the extensions.. anyway.. see below.
/*
To compile: g++ -g -o c c.c
To execute: ./c 100000 10 1000
*/

............... snip .................
for(j=0; j<times; j++) {
for(i=0; i<max; i++) {
std::string *v = new std::string();
std::string *k = new std::string();

here you are obtaining two strings using new... I dont see that you are
deleting them so far.. so it is not surprise you have a memory leak.
// --- WANT TO FREE THIS ---
sprintf(t, "%06d", i);
v->assign(t);
k->assign(t);
num[k->c_str()] = v;

here you can delete both v and k since the keys and the values will be
passed to the container by value and it will have its own private copy that
it will manage by itself..

I dont see how is that you dont delete both... maybe I am not understanding
something else you want to do.

If you want your containers to contain references instead of the objects
themselves then store pointers... not objects.

Regards

Roberto
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top