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?

2
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.

Kon



#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;


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() {
char hold;

HASH_MATRIX HM;
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;
cin.get(hold);

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






// 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;
cin.get(hold);

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

return 0;
Oct 30 '06 #1
3 3687
dtimes6
73
U can put ur code in a "{}" that will work.
Oct 30 '06 #2
kony
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.
Regards

Kony
Nov 1 '06 #3
dtimes6
73
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

2
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...
5
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & ...
20
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...
4
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...
1
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, ...
2
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...
1
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...
5
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; ...
2
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...
0
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...
0
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...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
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...
0
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...
0
jalbright99669
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...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
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. ...
0
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.