Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   hash_map deallocation problem (http://www.go4expert.com/forums/hashmap-deallocation-problem-t1101/)

kony 1Aug2006 04:41

hash_map deallocation problem
 
Hi there,

I am experiencing a problem with deallocation of a hash_map container. Here is the example code:

Code: C

#include <functional>
#include <numeric>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <climits>
#include <ext/hash_map>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <blitz/array.h>


using namespace std;
using namespace blitz;


using namespace __gnu_cxx;


typedef pair <int, int> Coordinates;
namespace __gnu_cxx
{
struct hash <Coordinates>
{
size_t operator()(const Coordinates& c) const
{
return (c.first << 24) + c.second;
}
};
}


typedef hash_map <Coordinates , double> HASH_MATRIX;


void
constructHashMatrix(HASH_MATRIX& hashMatrix,
const int& i,
const int& j,
const double& value) {
// create a pair
Coordinates ij(i,j);
// search for the pair in the table
// Coordinates i,j already exist. Locate this record and add to its current value
hash_map <Coordinates , double>::iterator pos = hashMatrix.find(ij);
if (pos == hashMatrix.end()) {
hashMatrix[ij] = value;
}
else {
pos->second += value;
}
}


int
main() {
HASH_MATRIX HM;
HASH_MATRIX::iterator p;
int n = 20000000;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 100; j++) {
double value = 3.14;
constructHashMatrix(HM, i, j, value);
}
}
HM.clear();
return 0;
}

When I use HM.clear() the system releases some memory but the majority (75%) is still in use. I want to destroy the whole object and release the memory for use by another object. I would greatle appreciate your help. Thanks in advance.

Sincerely,

Kon


All times are GMT +5.5. The time now is 06:22.