-1

I am using unordered maps in C++11 that have keys which are concatenated strings of various numbers and characters. An example of a key that I am using is:

coeff_key = "_"+to_string(first_k_index) +"_"+ to_string(second_k_index) +"_"+ to_string(i) +"_"+ to_string(j)+"_";

I can fill all the maps, but once I try to access a certain key, there is a problem. Furthermore, if the values of first_k_index can be 0-3, no error appears. But if the values of first_k_index can be 0-9, the code does not run completely. Here is the output from gdb.

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Program received signal SIGABRT, Aborted.
0x00002aaab1d2a387 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-307.el7.1.x86_64 libgcc-4.8.5-39.el7.x86_64 libstdc++-4.8.5-39.el7.x86_64
(gdb) where
#0  0x00002aaab1d2a387 in raise () from /lib64/libc.so.6
#1  0x00002aaab1d2ba78 in abort () from /lib64/libc.so.6
#2  0x00002aaab15357d5 in __gnu_cxx::__verbose_terminate_handler() () from /lib64/libstdc++.so.6
#3  0x00002aaab1533746 in ?? () from /lib64/libstdc++.so.6
#4  0x00002aaab1533773 in std::terminate() () from /lib64/libstdc++.so.6
#5  0x00002aaab1533993 in __cxa_throw () from /lib64/libstdc++.so.6
#6  0x00002aaab1533f2d in operator new(unsigned long) () from /lib64/libstdc++.so.6
#7  0x00002aaab1533fc9 in operator new[](unsigned long) () from /lib64/libstdc++.so.6
#8  0x00000000004035b6 in main ()
  • This question's shown code does not meet stackoverflow.com's requirements for a [mre]. This means it's unlikely that anyone here can conclusively answer the question; but only guess at the most. You should [edit] your question to show a minimal example, no more than one or two pages of code (the "minimal" part), that everyone else can cut/paste, compile, run, and reproduce the described issue (the "reproducible" part) ***exactly as shown*** (this includes any ancillary information, like the input to the program). See [ask] for more information. – Sam Varshavchik Sep 16 '20 at 01:12
  • 1
    That has `operator new[]` in the callstack, but standard containers use `std::allocator::allocate`, which calls `operator new` only, so it's probably somewhere else in the code – Artyer Sep 16 '20 at 01:13

1 Answers1

0

There are multiple possible causes, the most probable being that there is not enough memory to construct the map. Also See: Debugging strategy to find the cause of bad_alloc

Docs

Elias Schablowski
  • 2,619
  • 9
  • 19