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
3
votes
1 answer

QString throws bad_alloc Exception

I've got a problem. I use the following Code to convert a std::string into a QString. std::string testStdStr = "Hello"; QString test = QString::fromStdString(testStdStr); This code throws a bad_alloc exception unter MSVC 2013 Prof, but only in…
Kevin H. Klein
  • 403
  • 1
  • 5
  • 14
3
votes
3 answers

C++ Vector catch resize Memory Leak

I am trying to save a vector full of pointer to Circle objects. Sometimes the bad_alloc catch works, but sometimes it doesn't, then I get the error message: This application has requested the Runtime to terminate it in an unusual way. Please…
Madeye
  • 55
  • 2
  • 6
3
votes
1 answer

Bad alloc is thrown

I am getting a 'bad_alloc while trying to work with boost managed shared memory. I've copied the boost example from their quick guide for the impatient and incorporated my own changes. MY code is below, I've commented out the example stuff and wrote…
user3208010
  • 45
  • 1
  • 8
3
votes
1 answer

Is there a nothrow new that waits until memory is available?

Before reading this question, I had never taken exception handling seriously. Now I see the necessity but still feel "Writing exception safe code is very hard". See this example in that question's accepted answer: void doSomething(T & t) { …
jingyu9575
  • 521
  • 3
  • 16
3
votes
2 answers

Operator new and bad_alloc on linux

On Linux, malloc doesn't necessarily return a null pointer if you're out of memory. You might get back a pointer and then have the OOM killer start eating processes if you're really out of memory. Is the same true for c++'s operator new or will…
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
3
votes
1 answer

How to throw std::bad_alloc with a custom message in C++?

Since VS2008 the class bad_alloc does not provide a constructor with string parameter. Is there any possibility to create a custom message without overwriting the class like this? // this is only pseudo-code class custom_exception : bad_alloc…
alex555
  • 1,676
  • 4
  • 27
  • 45
3
votes
2 answers

Linux C++ program crashes with St9bad_alloc after map gets very huge

I am running a C++ program that involving building inverted index on red hat linux 64 bits. My invert index is defined as map > invertID; and I got this error where it crashes randomly, with what(): …
Karl
  • 5,613
  • 13
  • 73
  • 107
3
votes
1 answer

cuda/thrust: Trying to sort_by_key 2.8GB of data in 6GB of GPU RAM throws bad_alloc

I have just started using thrust and one of the biggest issues I have so far is that there seems to be no documentation as to how much memory operations require. So I am not sure why the code below is throwing bad_alloc when trying to sort (before…
Sven K
  • 159
  • 5
2
votes
2 answers

Out of memory - why does the system not page out not recently used?

I have a program which does some very intensive graphics work, and requires a lot of memory (> 8GB), however my machine has only 8GB of RAM currently, which means the program throws a bad alloc when it runs out of memory. Besides this program, I had…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
2
votes
2 answers

"X Error" BadAlloc GLX BadContext on IntelSandyBridge (Intel HD Graphics 3000)

I'm running a debian stable ThinkPad X1 (1294-3QG) with exactly three packages from squeeze-backports needed for the GraphicsModi: initramfs-tools 0.99~bpo60+1 linux-base 3.4~bpo60+1 linux-image-3.2.0-0.bpo.2-amd64 3.2.9-1~bpo60 While running that…
Bastian Ebeling
  • 1,138
  • 11
  • 38
2
votes
2 answers

MySQL C++ Connector memory overflow error

I've been trying to hook up to my own locally hosted MySQL database with the MySQL/C++ Connector package. The lines that are really giving me a problem are: driver = get_driver_instance(); auto_ptr < Connection > con (driver ->…
Mister Mister
  • 33
  • 1
  • 8
2
votes
0 answers

Why am I getting an std::bad_alloc in my code?

I'm writing a function that loads data from a file into memory so that I can use it within my program on demand. The size of the data file (in bytes) is read from a separate JSON file. This is the relevant code that gives me issues: const uint32_t…
enzeys
  • 108
  • 9
2
votes
5 answers

C++ array of derived class vs array of pointers of base class to derived objects - why is amount of memory allocated so much different?

I need some clarification on an issue I don't quite understand. Using the two scenarios that follow, I would have thought that the amount of memory allocated would roughly be the same. However, scenario 2 gives me a bad_alloc exception after a while…
hoppla
  • 51
  • 5
2
votes
0 answers

Eigen on STM32 works only until a certain size

I am trying to use Eigen C++ library on STM32F4 Discovery embedded board to perform some matrix operations in the future, specifically to do some kalman filtering on sensor data. I tried linking against the standard c++ library and even tried to…
the_parzival
  • 354
  • 3
  • 19
2
votes
3 answers

c++ bad_alloc exception not catched

I have following code to allocate a huge amount of data and if it is more than there is memory available (here it is 32GB) it should throw an exception. Using: bool MyObject::init() { char* emergency_memory = new char[32768]; try { …
goaran
  • 353
  • 2
  • 11