I have a big project, compiles fine, but sometimes when I run in terminal(sometimes not)
./run
it gives bad_alloc exception, so i think it might be helpful to backtrace using gdb, so I do
gdb ./run
run
and it weirdly exit normally, nothing wrong shows up, even though I tried a lot of times. Has someone met similar issues before?
See this answer for what may be different "inside" vs. "outside" of GDB, and what to do about it.
If you can enable core dumps (ulimit -c unlimited
), that should give you another way to get the stack trace.
In my experience, most bad_alloc
s result from one of two root causes:
Uninitialized size:
int size;
if (something) {
// assign to size here
}
std::vector v(size); // Oops: size may be unintialized.
Arithmetic underflow:
std::vector v(other_vector.size() - 20);
Here, if other_vector.size() < 20
, you'll get humongous value.