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

vector is throwing bad_alloc

I have the following code that is throwing a std::bad_alloc exception: std::vector> myVector(nlines); for(int i = 0; i < nlines; i++) { try { std::vector iVector(ncolumns); …
Ben
  • 3,241
  • 4
  • 35
  • 49
4
votes
3 answers

Why am I getting "Invalid Allocation Size: 4294967295 Bytes" instead of an std::bad_alloc exception?

I wrote the following piece of code to allocate memory for an array: try { int n = 0; cin >> n; double *temp = new double[n]; ... } catch(exception& e) { cout << "Standard exception: " << e.what() << endl; exit(1); } Of…
StarFighter
  • 69
  • 1
  • 8
3
votes
1 answer

Why don't I get std::bad_alloc in my WinCE application?

According to C++ Standard, operator new should throw std::bad_alloc(); when allocation fails. To test this behavior I came up with the following code: try { for (;;) { Big* p = new Big; if (0 == p) { …
sthlm58
  • 1,142
  • 1
  • 9
  • 28
3
votes
3 answers

memory allocation vs. swapping (under Windows)

sorry for my rather general question, but I could not find a definite answer to it: Given that I have free swap memory left and I allocate memory in reasonable chunks (~1MB) -> can memory allocation still fail for any reason?
Jakob S.
  • 1,851
  • 2
  • 14
  • 29
3
votes
2 answers

std::bad_alloc not getting caught in any calling stackframe

If the new operator fails to allocate memory, the exception std::bad_alloc is only getting caught if I put a try-catch block immediately surrounding the new statement. If I instead have the try-catch block in a caller a few stackframes down below,…
Walking Corpse
  • 107
  • 7
  • 31
  • 49
3
votes
1 answer

PCL octree std::bad_alloc c++

I am using the PCl library to compress lidar data. This data is then sent via the ROS network with a custom message. For this, I have a compression node and a decompression node. If I run the compression node, everything works as expected. The…
user71649
  • 31
  • 1
3
votes
1 answer

How do I fix Error: std::bad_alloc message when using DPLYR full_join to join 3 large data frames?

I have 3 very large files with thousands of observations (file_1 = 6314 rows, file_2 = 11020 rows, file_3 = 2757 rows). I need to join them, so I used the function full_join() from the dplyr package. When I run the code I get this error:Error:…
Kristen Cyr
  • 629
  • 5
  • 16
3
votes
1 answer

std::bad_alloc when adding a struct to std::vector

this is probably something stupid, but i can't figure it out. I'm getting a std::bad_alloc exception in the following code snippet (which is a case statement in a switch): case 0: { MyPrimitiveNode* node = new MyPrimitiveNode( 1, false…
PeterK
  • 6,287
  • 5
  • 50
  • 86
3
votes
1 answer

bad_alloc with unordered_map initializer_list and MMX instruction, possible heap corruption?

I am getting a bad_alloc thrown from the code below compiled with gcc (tried 4.9.3, 5.40 and 6.2). gdb tells me it happens on the last line with the initalizer_list for the unordered_map. If I comment out the mmx instruction _m_maskmovq there is no…
Eric Roller
  • 429
  • 5
  • 19
3
votes
2 answers

On new's bad allocation error, does delete still need to be called?

When using new and a bad_alloc exception is thrown. Do you still need to call delete on the ptr before carrying on or can you be confident that no memory has been allocated? How about if you use the nothrow version? can you again be confident that…
uhsl_m
  • 322
  • 3
  • 11
3
votes
2 answers

bad_alloc upon inserting object by rvalue and object validity

Consider the following code (assume that SomeClass has move constructor and fields like pointers which would go invalid after object gets its content swaped with another object): SomeClass data; std::list queue; try {…
PookyFan
  • 785
  • 1
  • 8
  • 23
3
votes
1 answer

Mergesort - std::bad_alloc thrown when trying to assign vector

Good afternoon ladies and gents. So, it is not my day for errors. Implementing Mergesort (not in-place) in C++, and I'm having real trouble with the code, with no idea why. The second-to-last line of the mergeSort() function assigns the result of…
Stephen
  • 6,027
  • 4
  • 37
  • 55
3
votes
2 answers

operator new doesn't return 0 when running out of memory

I found this interesting exercise in Bruce Eckel's Thinking in C++, 2nd ed., Vol.1 in Chapter 13: /*13. Modify NoMemory.cpp so that it contains an array of int and so that it actually allocates memory instead of throwing bad_alloc. In main( ), set…
Janos
  • 650
  • 8
  • 13
3
votes
1 answer

Throw Bad_alloc exception if not enough memory

I just need to be sure I have enough memory available to the bi-dimensional array map. So I think I'd have to put a try catch bad:alloc in every row of map or perhaps with no throw would be more elegant. But I can't get with it. Any help would be…
3
votes
1 answer

std::bad_alloc after replacing boost:python function wrapper with Python/C API

I had a function in C which I used to extend python, previously using the BOOST_MODULE function to accomplish this. This error came up when transitioning to the python-C API. I am certain that the run_mymodule function runs fine without this…
kilojoules
  • 9,768
  • 18
  • 77
  • 149
1 2
3
17 18