Hi,

I wish to know that the cause of the following error when i run the program :
a) Is memory leak in the program
B) The system on which i am running the program has insufficient memory.

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

Thanks

Dani AI

Generated

If the program throws std::bad_alloc but you cannot find the failing line in 3000 lines, focus first on locating the throw, then on what size is being requested.

Use a debugger to break on throws so you get an exact stack trace (no need to paste all code). In GDB: run catch throw then run; when it stops use bt to inspect the call stack. In Visual Studio enable breaking on C++ exceptions in the Exception Settings window. These steps give the function and line that triggered the allocation attempt (GDB catchpoints, ).

Once you know the site, log or print the exact size passed to reserve, resize, or new. Typical root causes are incorrect size arithmetic (overflow), signed-to-unsigned conversions creating huge counts, malformed input producing enormous sizes, or an accidental loop that keeps pushing until memory is exhausted. A small diagnostic wrapper can help:

try {
    v.reserve(n);
} catch (const std::bad_alloc& e) {
    std::cerr << "reserve(" << n << ") failed: " << e.what() << "\n";
    throw;
}

If you suspect leaks or gradual growth, run the program under a memory tool (Valgrind, Dr. Memory) to find retained allocations and growth patterns. Also check whether you are building 32-bit (limited address space) versus 64-bit — that affects available address space but does not fix logic bugs that request absurd sizes. See the standard notes on allocation failure and vector capacity for behavior details (, vector::reserve).

Combine these techniques with ’s suggestion to break the code into smaller units; isolating the allocator call makes it trivial to fix the wrong size calculation rather than masking the symptom by catching the exception globally.

Recommended Answers

All 3 Replies

A bad_alloc exception is thrown when dynamic memory allocation has failed, I think this is often the case when your system has not enough memory, or are you allocating to much memory ?

A memory leak could be, but me eyesight is not good enough to see your program's source code from here :P

A bad_alloc exception is thrown when dynamic memory allocation has failed, I think this is often the case when your system has not enough memory, or are you allocating to much memory ?

A memory leak could be, but me eyesight is not good enough to see your program's source code from here :P

My code has 3000 lines and i do not know in which function the error is..so i did not paste the code.

3000 lines? are they all in main()? If that is the case that is your 1st problem. 3000 lines or 30,000 lines want matter if you break the code down.

Get into the habit of writing smaller methods. I usually try to keep my methods as simple and small as possible. 1 operation per method. Usually for me an average method is around 15 -20 lines of code. Some are of course more. But generally when you do big project break your project down to smaller "programs" and code those "programs" and finally integrate them, which usually doesn't take as much time as trying to figure out an error in a 3000 line code.

As for this error. How much memory does your machine have? If you think lack of memory is the issue then try closing some background programs. When you run program check how much memory is being used by your program.

I think its not a memory leak but you are trying to access a memory location that doesn't exist. Just a thought.

If you can figure out where the error occur and post someone here will be able to help.

Go to where you create the vector and then use the vector + then the error line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.