473,803 Members | 3,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I deallocate memory from a hash_map?

2 New Member
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()(cons t Coordinates& c) const
{
return (c.first << 24) + c.second;
}
};
}


typedef hash_map <Coordinates , double> HASH_MATRIX;


void
constructHashMa trix(HASH_MATRI X& 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>::iterat or pos = hashMatrix.find (ij);
if (pos == hashMatrix.end( )) {
hashMatrix[ij] = value;
}
else {
pos->second += value;
}
}


int
main() {
char hold;

HASH_MATRIX HM;
HASH_MATRIX::it erator 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;
constructHashMa trix(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 3844
dtimes6
73 New Member
U can put ur code in a "{}" that will work.
Oct 30 '06 #2
kony
2 New Member
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 New Member
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
1928
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 one of my class, say myClass, as data. I give the hash_map a comparator for the strings that compares the contents of the string instead of the address of the strings (by default hash_map compares the addresses of the strings (!)).
5
8626
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
20
9376
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 approximately 80Mb of memory.
4
1736
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 this: hash_map<char*, inta; char* b = new char; int i; //some code that initializes b and i a = i; i do that for a huge amount of data and end up with 470 MB of memory for the table. now, if i got it right, it should actually use (17*1+4)*3700000 = 78
1
9404
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, it is not available in Apache since it is not standard c++. Though it is available with GNU STL. The code module where I use hash_map will generate separate object file during compilation. This code module is also using STL string.
2
4284
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 "node_hasher.h" class Node; typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
1
4476
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 ~5million key-value pairs, and needs to be accessed often to store in memory, its not updated during the program. B <int,hash_map<int,float>> which is also ~5m key-value pairs, but I can read this in parts from the disk. The inner hash_map has about 20 elements. In my program, I read in A and...
5
3376
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
8152
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 interchanged? What would it take to switch from hash_map to unordered_map? Thank you. -Marek
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.