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

Does new set pointer to NULL if it throws std::bad_alloc?

I know doing a new(std::no_throw) will set pointer to NULL if it failed. I also know that a normal new will throw a std::bad_alloc exception if it failed. Will the normal new also set the pointer to NULL if it throws? Or should I set it to NULL in…
Pubby
  • 51,882
  • 13
  • 139
  • 180
2
votes
1 answer

expression templates - bad_alloc

i am currently working on a c++ project and now i am stuck already for a while. It's about delayed evaluation with expression templates and (for me at least) a strange bad_alloc. If you try the code below, you'll notice runtime error bad_alloc due…
user8705939
2
votes
2 answers

How to achieve, that bad-alloc aborts instead of throwing an exception

In our application, we log any crashes into a log file with stack trace included. We can use these reports to identify crash causes. The problem is, that we tend to catch std::exception on several places (a lot actually), which makes the report…
kovarex
  • 1,766
  • 18
  • 34
2
votes
1 answer

.push_back() on large vector causes std::bad_alloc

This was compiled on 64 bit VS C++ 2015. std::bad_alloc occurs where x is specifically 1120 static std::vector>> g_damagefunction; static std::vector> g_has_damagefunction; static…
Carlos J
  • 23
  • 5
2
votes
1 answer

Crash in QString toUtf8()

I have a slot: void Foo::func(QString str1, const QString& str2, int width, int height) { std::unique_lock _lock(m_mutex); #ifdef _DEBUG MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); if…
user2123079
  • 656
  • 8
  • 29
2
votes
1 answer

why bad_alloc when new array, resize vector and push_back to vector?

I need to create a very big array in my project. I tried 3 methods, but all of them turned out to be bad_alloc. I couldn't understand, as my PC's RAM is 10GB. Here are my implementations under MSVC2015 x86…
user1024
  • 982
  • 4
  • 13
  • 26
2
votes
3 answers

Reading large (~1GB) data file with C++ sometimes throws bad_alloc, even if I have more than 10GB of RAM available

I'm trying to read the data contained in a .dat file with size ~1.1GB. Because I'm doing this on a 16GB RAM machine, I though it would have not be a problem to read the whole file into memory at once, to only after process it. To do this, I employed…
glS
  • 1,209
  • 6
  • 18
  • 45
2
votes
1 answer

Why is std::bad_alloc thrown when enough memory is available?

The test code as follows and compiled by vs2013: #include #include #include int main() { uint32_t one_size = 32 * 1024; uint64_t total_size = 0; auto deleter = [](char* p) { delete[] p; }; using…
Bruce
  • 61
  • 1
  • 9
2
votes
2 answers

how to cause bad_alloc

I have to cause bad_alloc for my unit test (basically, for 100% code coverage, there's no way i can change some functions). What should I do? Here is my code example. I have to cause bad_alloc somewhere here. bool insert(const Value& v) { Value…
2
votes
0 answers

Error when using Decision Trees in OpenCV 3.0.0-rc1

I am doing some machine learning in OpenCV and i'm using Decision Trees. I am currently using OpenCV 3.0.0-rc1. Whenever i attempt to train Decision Trees with my training data and labels, i get either terminate called after throwing an instance…
2
votes
0 answers

Why std::bad_alloc may end up not being caught?

So I'm currently looking into std::bad_alloc and its behavior, and there's something I don't understand. This very simple snippet: try { char* p = new char[10000000000ul]; } catch(std::bad_alloc& e) { std::cout << "Caught a bad alloc" <<…
JBL
  • 12,588
  • 4
  • 53
  • 84
2
votes
4 answers

c++ stl priority queue insert bad_alloc exception

I am working on a query processor that reads in long lists of document id's from memory and looks for matching id's. When it finds one, it creates a DOC struct containing the docid (an int) and the document's rank (a double) and pushes it on to a…
bsg
  • 825
  • 2
  • 14
  • 34
2
votes
1 answer

Catch C++ exceptions on AIX with dbx

I have a C++ application which terminates with a "bad allocation" error message for certain input data on an AIX machine. Is there a way to run the program in dbx and catch the exception when it's being thrown? I don't see anything about it in IBM's…
piokuc
  • 25,594
  • 11
  • 72
  • 102
2
votes
1 answer

bad_alloc identifier not recognized?

I'm getting a compiler error: error C2061: syntax error : identifier 'bad_alloc' I've used the chunk of code below in other projects before, with different memory allocation within the try block, without problems. I was hoping someone could explain…
2
votes
2 answers

std::bad_alloc ulimit

I'm working on a 32bit Ubuntu 12.04. The RAM is 7.7 GB. When I run a certain process (written in C++) and reaches 50% of RAM usage, such process throws a "std::bad_alloc". The strange thing is that heap size is unlimited (command ulimit -v returns…
user1718482