1

So I'm making a program that reads through 10,000 lines of code. I have an unordered_map of vectors, and each line adds data to that unordered_map (either in the form of a new key or adding additional data to one of the vectors).

For each line, I'm storing data using emplace. For example, I'll have a

vector <int> temp;

that I store data in using

temp.push_back(someInt);

and then store that vector in my unordered_map with

uList.emplace(someKey, temp);
temp.clear();

or I add data to one of the map vectors using

uList[i].push_back(someInt);

This happens multiple times per line, then I move on to the next line and do it all over again, 10,000 times.

The error I get is

"terminate called after throwing an instance of 'std::bad_alloc'"

I'm assuming it's because of problems allocating memory with my unordered_map or the vector I have.

My question is: Can an unordered_map have issues allocating memory if I use .emplace() too many times, and are there any common practices used to prevent bad allocation with vectors or unordered maps?

Any advice is appreciated, even calling me out on me saying something stupid would help!

  • You may want to use a `std::move(temp)` - since you've named your temporary, by default C++ won't move from it. This also means there wouldn't be a need to `clear` the moved-from vector. – MSalters Feb 02 '17 at 10:01

1 Answers1

1

I don't think there's enough here to know why you're getting bad_alloc. Typically it just means you've run out of memory.

My first thought is that you have a logic issue where you're allocating a lot more than you think. I also wonder whether the hash generated for the unordered map is poor, causing you to insert a lot more elements rather than updating the list for existing elements.

My suggestions: post a more complete sample, or add some debugging/tracing to see just how many items are being created and stored in your data structures.

Also, just how much memory is your process consuming before it terminates? Modern (especially 64-bit) processes running on operating systems featuring virtual memory can allocate a considerable amount before this fails. The limit for 32-bit is more likely to be reached but it's still considerable.

  • Thank you, I'm going to try to put together a more complete sample. It's 32-bit. I'm not too experienced with unordered maps. It could have something to do with me having a vector to store numbers for each line in addition to another empty vector that I use in the .emplace() to add an element to my unordered map, which eventually is filled with multiple vectors that can be like 600 elements long. – Alessandro Lorusso Feb 02 '17 at 08:11