The exception std::bad_alloc means " operator new failed". Thus, either operator new receives a call to operator* on newObj (which we donβt know anything about), or a map insert operator (which is more likely).
In particular, when you call operator[] on a map with some parameter k
If k does not match the key of any element in the container, the function inserts a new element with this key and returns a link to its displayed value. Please note that this always increases the container size by one, even if the item has no associated value (the item is created using its default constructor).
(as described here ).
Map::operator[] provides a reliable failure guarantee:
Strong guarantee: if an exception is thrown, there are no changes in the container.
but does not guarantee that the exception will not be thrown (i.e., it does not guarantee the absence of failure).
The reason operator new throwing exceptions can be of a different nature. However, it all boils down to:
throws bad_alloc if it fails to allocate storage.
However, as James Canze says in the comments:
Another possible reason for std :: bad_alloc is undefined behavior. If for example, he ruined the arena of free space. And realistic, if he really runs out of memory, the distribution where it fails will be different. If systematically here, I would suspect a problem in the constructor of copying an object, more than anything else.
means that operator new cannot allocate storage due to some error in other parts of the program. You can debug your null information (as statistics called it) by allocating (very) large amounts of data immediately before calling operator[] . If the dummy distribution does not work, you can say that the error with the copy constructor is with good certainty.