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");
}
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");
}