472,331 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

How do I deallocate memory from a hash_map?

Hi there,

I would much appreciate your help with the following problem.
Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently clear() function is not working and the trick with swap() is half working. Does anybody know how to deallocate the hash_map? Thanks in advance.


#include <functional>
#include <numeric>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <climits>
#include <ext/hash_map>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iterator>
#include <iostream>

using namespace std;

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;

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;

main() {
char hold;

HASH_MATRIX::iterator p;
// fill in the hash matrix
int n = 200000;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 10; j++) {
double value = 3.14;
constructHashMatrix(HM, i, j, value);
// at this stage I use about 70MB memory
cout << " Hash matrix constructed: Press enter to continue : " << endl;

// at this stage I use again about 70MB memory
cout << " Hash matrix clear() : Press enter to continue : " << endl;

// Swap trick to clear and get rid of capacity, too

hash_map <Coordinates , double>().swap( HM );
// at this stage I use again about 46MB memory

cout << " Hash matrix deallocated : Press enter to continue : " << endl;

// however, I want to release all the memory from the hash_map

return 0;
Oct 30 '06 #1
3 3687
U can put ur code in a "{}" that will work.
Oct 30 '06 #2
U can put ur code in a "{}" that will work.
You can try that yourself. It is not working, it releases abot 30% of occupied memory. I am solving huge system of equations and memory management is important. So if you know some other trick I'll greatly appreciate your help.

Nov 1 '06 #3
This seems as if the memory pool did not return the memory back. U may try impl ur Alloc for ur hash map.
Nov 1 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: rolivawdaneel | last post by:
Hello, I am using SGI's hash_map in such a way that there is an inherent memory leak. Let me explain: 1- My hash_map takes char* as keys and...
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & ...
by: mariano.difelice | last post by:
Hi, I've a big memory problem with my application. First, an example: If I write: a = range(500*1024) I see that python process allocate...
by: filox | last post by:
so, i'm using the hash_map that comes with MS visual studio, and it seems to spend an awful lot of memory. what i basiclly have is something like...
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, ...
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include...
by: mono12 | last post by:
Hi! I have written code using STL hash_map in c++ and am running into memory issues. I have two kinds of hash_maps: A <int,int> which stores...
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: float a; float b; ...
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be...
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.