# hash_map deallocation problem

Discussion in 'C' started by kony, Aug 1, 2006.

1. ### konyNew Member

Joined:
Aug 1, 2006
Messages:
1
0
Trophy Points:
0
Hi there,

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

Code:
```#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