Is there a more frequent error that is memory-related that throws bad_alloc? I understand that it means that memory-allocation has failed, but what is the most common mistake that leads to this in code?
EDIT: The other commenters have pointed out a few interesting scenarios. I'm adding them to my response for the sake of completeness.
My understanding is that bad_alloc is thrown whenever the operators new and new[] fail to allocate memory to an object or variable. This can happen if you've newed a bunch of objects and forgot to delete them before they got out of scope (i.e., your code leaks like crazy).
There might be several free memory blocks available, but none of these are large enough.
bad_alloc is thrown if you pass a negative value as its parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With