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
-1
votes
2 answers

Terminate called after throwing an instance of std::bad_alloc. Using two classes, one with a pointer to the other

My program keeps getting me bad alloc error when I use a normal function that contains a member function. The program is about taking some specific inputs from the command line and printing the elements of an array of pointers. This has to be done…
-1
votes
1 answer

bad alloc exception when trying to resolve BFS challenge on HackerRank

So i was trying to make the challage: Breadth First Search: Shortest Reach on HackerRank, but i keep getting the bad alloc exception when the tests have great numbers of node/edges. The program works on the first test, so i don't think, it's…
-1
votes
1 answer

C++ catch bad_alloc not working

I have a C++ simple queue console application, first display three main choices and ask user to choose: 1 enqueue, 2 dequeue, 3 exit. When user selects 1, it calls enqueue function and prompt user for input and then do the rest. The program is…
-1
votes
1 answer

std::bad_alloc when inserting into std::unordered_map?

I'm sometimes getting an std::bad_alloc for the following code: typedef std::unordered_map map_type; map_type m_foo; // the transgressor: auto r = m_foo.insert(map_type::value_type(id, bei)); It happens some of the…
-1
votes
1 answer

bad_alloc exception while implementing the merge sort for larger arrays

I'm implementing the merge sort algorithm using C++. An exception(bad_alloc) is raised, while sorting larger arrays. Since i'm new to C++ i have no idea about how to get rid of this error. The answer i'm willing is not handling the exception, but…
Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
-1
votes
1 answer

C++ std::bad_alloc error with std::vector

I understand this question has been asked before, but the answers seemed too specified towards the asker's code. If this is considered a useless duplicate question feel free to remove it or mark it as a duplicate. As far as I can tell, my code is…
Jamie Blue
  • 52
  • 1
  • 10
-1
votes
1 answer

C++ Deck and Card Class Error with bad alloc

Just started learn to code in school. Our assignment requires us to create a card game with card,deck and hand class. I am having troubles with it now and i keep getting exception: std::bad_alloc at memory location. Here are my codes right…
-2
votes
1 answer

C++ How to stop over-allocating in heap?

I have the following code: #include int main() { try { while (true) int* ptr = new int[10000000L]; } catch (const std::bad_alloc& e) { std::cout << e.what(); return EXIT_FAILURE; } …
-2
votes
1 answer

C++ Code is throwing std::bad_alloc, i am trying to receive screenshots through socket and display to a gui window win32api

C++ Win32Api GUI, I Am trying to display received screenshots on server from client. I am trying to create a Remote Desktop Viewer. Client is capturing screenshots and then sending them to the server, where i am trying to receive and display to a…
Rocka
  • 59
  • 7
-2
votes
1 answer

bad_alloc error for std::vector resize on 64-bit. It seems to be limiting it to 32 bits

Why do I get bad_alloc error? It appears to be limiting me to 32 bits but it is a 64 bit machine and compiler. I am using this to store large sets of data which are in the vector int array. I tried setting heap and stack during compiling but this…
Alvin Reed
  • 19
  • 3
-2
votes
1 answer

Throw bad_alloc if there's not enough memory

/ * * \ brief Dictionary builder from file * * \ pre There is enough memory * \ pre The file is first opened * * \ post If the file is open, the class instance has been initialized from * from the dictionary file. Otherwise, we generate an empty…
Robert
  • 71
  • 1
  • 6
-2
votes
1 answer

bad alloc c++ loading file in matrix

I try to load a large file (20gb) and load it into a matrix. However I get a bad_alloc error when it tries to load the file in the matrix. My code is working on Mac but doesn't on Linux. Here is my code: std::ifstream ifs(filename,…
Paul Bénéteau
  • 775
  • 2
  • 12
  • 36
-2
votes
1 answer

C++ threads and class methods. Problem with memory leakage

I have a problem with threading in C++, I want to parallelly call same method on different class objects. However, program encounters some problem with that. From time to time it crashes and sometimes runs through code part, but always return a…
Padzak
  • 31
  • 6
-2
votes
1 answer

'std::bad_alloc' when trying to delete priority_queue elements C++

When returning a ship to the port, speed becomes 0.0 and user inputs shield and fuel. –If fuel is 0.0, the ship gets destroyed –Ships still in the priority_queue take 10 shield damage and lose 15 fuel –If shield or fuel become less than 0.0, the…
-2
votes
1 answer

bad_alloc C++ while using std::string constructor with char pointer

I'm in the middle of creating a simple program with C++ (11). My issue is that function called "loadRoomsFile()" is reading text from a file to char pointer array and then return it. I want to convert it to a string so I have used string constructor…
1 2 3
17
18