473,789 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hash_map in stl c++ memory issues

2 New Member
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<i nt,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 B1(first 100K key-values), I perform the required computation, and say B1.clear(), and read the next set of 100k elements.

1) The program exits with an std::bad_alloc error. How can I fix this?

2) What hash_fns would be good for this large data?

3) It seems that clear does not free the memory. What can I do? When I say B1.clear(), will it free the inner hash_map also?

4) How can I find out what is using up the memory?

I am a novice to stl programing, any help would be appreciated.

Thanks!
Mar 18 '08 #1
1 4474
weaknessforcats
9,208 Recognized Expert Moderator Expert
1) The program exits with an std::bad_alloc error. How can I fix this?

2) What hash_fns would be good for this large data?

3) It seems that clear does not free the memory. What can I do? When I say B1.clear(), will it free the inner hash_map also?

4) How can I find out what is using up the memory?
1) You have run out of heap memory. Check your system and see what the heap limits are. That is, your program is in a process address space and you have to stay within that range of addresses. Becuae you have a 2GB machine does not mean you cna have a 2GB heap.

2) I have no idea. You need to research here. Check out The Art of Computer Programming by Donald Knuth Volume 2 - Sorting and Searching.

3) None of the STL containers gives up memory once it it acquired. This is by design. Allocating memory is slow operation and the STL is optimized for speed so once your container gloms onto memory, it keeps it. The B1.clear() just erases all of the elements in the container but does not reduce the memory size of the container. You can force the container to give up this memory by swapping it with a container that was created empty.

4) See (3) above.
Mar 18 '08 #2

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

Similar topics

5
4586
by: Sabrina | last post by:
Can someone help? I have been trying to get the hash_map in C++ for .NET to work with strings and const char*. I am using the const char* as the key and a pointer to another class as the data type. I would like to be able to assign unique names to each key and allow the user to be able to search the hash_map to find that key and in so doing, retrieve the information that is being pointed to by the other class' pointer(data type). I can...
5
20623
by: Charles Herman | last post by:
I have the folowing program: #include <iostream> #include <ext/hash_map> using namespace std; using namespace __gnu_cxx; typedef hash_map<char*, long, hash<char*> > char_hash_t;
2
2066
by: gibffe | last post by:
Hello I have a big problem with hash_map in stl. I'm trying to create hash_map<const char *, char *> map to keep key => val pairs as 4 byte C "strings". Everything is ok when i do let's say my_hash = "efg"; my_hash = "bb";
4
8075
by: Dymus | last post by:
problem with deleting big sized hash_map. have defined a comporator, where defined min_bucket_size (for speed improvement, since i know that i need to store big ammount of data (~1Mln)), everything seems to be good, working fast, but problems start when try to .clear() or delete that hash_map... I think that it just take ennormous amount of time to complete this thing. Waiting for possible suggestions for solvation of this problem
3
3844
by: kony | last post by:
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>
4
4125
by: lokki | last post by:
Hello, can anybody tell me what's wrong with following example code? char *k, *v; k = new char; strcpy(k, "a2"); v = new char;
4
3214
by: newbie | last post by:
For STL containers like hash_map, need I explicitly call clear() before delete()ing it? e.g. hash_map<string, MyClass**my_map; my_map = new (hash_map<string, MyClass*>); .... ....
6
6164
by: SimpleCode | last post by:
class COsgCar; class moving_vechicle; hash_map<moving_vechicle*, COsgCar*m_hash; hash_map<moving_vechicle*, COsgCar*>::iterator iter; iter = m_hash.find( pMovingVechicle ); if ( iter != m_hash.end() ) {
4
3415
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the Hashing component is in the Basic section). Or for a discussion and some benchmarks, http://kanze.james.neuf.fr/code/Docs/html/Hashcode.html. (That article is a little out of date now, as I've tried quite a few more hashing algorithms. But the...
0
9666
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
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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
7529
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
6769
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
5417
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
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.