How to deal with bad_alloc in C++?

In general you cannot, and should not try, to respond to this error. bad_alloc indicates that a resource cannot be allocated because not enough memory is available. In most scenarios your program cannot hope to cope with that, and terminating soon is the only meaningful behaviour. Worse, modern operating systems often over-allocate: on such systems, … Read more

Bad alloc is thrown

It seems you’re simply running out of memory. You might reason that you shouldn’t since the individual allocations don’t occupy the amount of space. But memory fragmentation can do this: if there is sufficient ‘padding’ or ‘overhead’ with the shared memory objects, you can run out of contiguously allocatable space. Either, store your data in … Read more