Questions tagged [bad-alloc]

std::bad_alloc is the type of exception thrown when memory allocation fails in a C++ program

In C++ when operator new or std::allocator cannot allocate memory an exception of type std::bad_alloc will be thrown.

265 questions
72
votes
7 answers

How to deal with bad_alloc in C++?

There is a method called foo that sometimes returns the following error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Abort Is there a way that I can use a try-catch block to stop this error from…
Nosrettap
  • 10,940
  • 23
  • 85
  • 140
45
votes
6 answers

Debugging strategy to find the cause of bad_alloc

I have a fairly serious bug in my program - occasional calls to new() throw a bad_alloc. From the documentation I can find on bad_alloc, it seems to be thrown for these reasons: When the computer runs out of memory (which definitely isn't…
Salami
  • 1,048
  • 1
  • 10
  • 17
31
votes
2 answers

"std::bad_alloc": am I using too much memory?

The message: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc I looked at the gdb backtrace and this is the lowest level method in there that I implemented myself: /* * get an array of vec3s, which will be…
Rooster
  • 1,267
  • 3
  • 15
  • 23
27
votes
4 answers

Is it okay to manually throw an std::bad_alloc?

I have this code.. CEngineLayer::CEngineLayer(void) { // Incoming creation of layers. Wrapping all of this in a try/catch block is // not helpful if logging of errors will happen. logger = new (std::nothrow) CLogger(this); …
Jookia
  • 6,544
  • 13
  • 50
  • 60
13
votes
3 answers

When a class dynamically allocates itself at constructor, why does stack overflow happen instead of std::bad_alloc?

I made a class that recursively creates itself using new (just for fun!), expecting that this will throw std::bad_alloc due to infinite dynamic allocation (heap overflow). But stack overflow happened instead of std::bad_alloc. Why does this…
ownfos
  • 165
  • 6
13
votes
2 answers

Never annotate functions involving dynamic memory allocation as noexcept?

Assume you have a function that normally can never fail, for example: std::string convert_integer_to_string(int x); In pricipal, this would be a candidate for noexcept. However, the implementation most likely involves involves dynamic memory…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
12
votes
1 answer

Why new[-1] generates segfault, while new[-2] throws bad_alloc?

I tried to test bad_alloc exception by passing some negative arguments to new[]. When passing small negative numbers I get what I hoped for - a bad_alloc. However, when passing -1, I can see that my object is constructed thousands of times (I print…
flyjohny
  • 273
  • 1
  • 2
  • 11
12
votes
3 answers

Why does my program occasionally segfault when out of memory rather than throwing std::bad_alloc?

I have a program that implements several heuristic search algorithms and several domains, designed to experimentally evaluate the various algorithms. The program is written in C++, built using the GNU toolchain, and run on a 64-bit Ubuntu system. …
Brad Larsen
  • 995
  • 1
  • 12
  • 20
11
votes
3 answers

How can I debug St9bad_alloc failures in gdb in C?

I have a program failing with: terminate called after throwing an instance of 'std::bad_alloc' what(): St9bad_alloc I imagine it's something to do with malloc/free, but I don't know which one. What breakpoint can I in gdb set that will break on…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
9
votes
1 answer

Strange std::bad_alloc

As far as I know, there are three reasons why a std::bad_alloc can be thrown: The process requests more memory than what can be served The address space is too fragmented to serve a request for a large chunk of contiguous memory The heap management…
gTcV
  • 2,446
  • 1
  • 15
  • 32
8
votes
1 answer

Can the std::vector default constructor throw an exception

If I construct an empty std::vector using the default constructor (and the default allocator), can it throw an exception? In general, allocating space for the elements of a container can throw an exception (which would be a std::bad_alloc). But the…
Raedwald
  • 46,613
  • 43
  • 151
  • 237
8
votes
1 answer

Bad Alloc with a 200GB memory available c++

I'm new to C++, and I'm studying 'compressive sensing' so I need working with huge matrices, and MATLAB is actually slow so I programmed my algorithm with C++. The thing is that I store big arrays (around 100Mb-1Gb). They are 20 arrays approx. and…
7
votes
1 answer

what(): std::bad_alloc - am I out of memory?

My dataset: 500,000 points in 960 dimensions. The size of the file is 1.9 GB (1,922,000,000 bytes). The code works for smaller data sets, but for this it will crash in the same point every time. Here is a minimal example: #include…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
6
votes
5 answers

Allocating large blocks of memory with new

I have the need to allocate large blocks of memory with new. I am stuck with using new because I am writing a mock for the producer side of a two part application. The actual producer code is allocating these large blocks and my code has…
JeffV
  • 52,985
  • 32
  • 103
  • 124
6
votes
2 answers

Best way to safely call new on this trivial example?

For a school project, I have 3 classes: an Egg, a Nest, and a Hen. We need to use new to create an instance of each in main, call display() on each, then explicitly delete each. That's all easy. My problem is not knowing how to properly catch a…
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1
2 3
17 18