Reputation: 357
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!
Upvotes: 1
Views: 973
Reputation: 133
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.
Upvotes: 1