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
3 answers

Why this vector constructor throw std::bad_alloc exception?

#include #include using namespace std; void test_bad_alloc(unsigned char *start) { try { vector v = vector(start + 1, start - 13); }catch (bad_alloc) { std::cout<<"bad alloc"<
Sichen Xu
  • 1
  • 2
-2
votes
2 answers

C++ vector std::bad_alloc error in a program

I was trying to sort a vector having user defined data type having two values, on the basis of one. But i get the error of bad_alloc. this is the code : #include #include #include using namespace std; struct s{ int…
jasmeet
  • 1
  • 1
  • 3
-2
votes
1 answer

va_arg causes std::bad_alloc

Whenever I run my program I get a std::bad_alloc exception thrown which causes an abort to occur. The std::bad_alloc ONLY gets thrown when va_arg is called. The odd thing is the code that crashes was provided by the instructor. I didn't write the…
Legion Daeth
  • 329
  • 1
  • 5
  • 15
-2
votes
1 answer

Why does the following code throw std::bad_alloc?

I have written code to test how many of the same numbers are present in both arrays, but for some reason it throws 'std::bad_alloc' could anyone explain why? It only throws it when N = 1000000 for some reason, is it because I have allocated 4000000…
Pavel
  • 1
  • 3
  • 17
  • 51
-3
votes
1 answer

terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc after pushing back into a vector

I'm facing a problem here, I've declared a pointer to a vector. After element insertion, it shows me this error terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Here is my code: vector*…
-3
votes
1 answer

I cant' seem to catch this exception in C++

For some reason the program exits when executed while testing the handling of an exception. This is the class im using as the exception recipient #ifndef _BADALLOC #define _BADALLOC #include using namespace std; class badalloc{ private: …
cercio
  • 41
  • 5
-3
votes
1 answer

c++ bad alloc applicaition failure

I have a piece of code where I dynamically create some relatively big objects (14MB) and populate a vector with them (of size 260). I am getting a bad alloc exception which I am catching. The application is still usable after my first throw, then…
JiLight
  • 3
  • 2
-3
votes
3 answers

bad_alloc when creating 20k array

I have to do some kind of project and I'm stuck. I'm getting an bad_alloc error. I checked code a lot of times, tried to google some solutions, but still nothing, that's why I'm writing here.The thing is the program runs normally, but at the task…
Robs
  • 153
  • 2
  • 10
-3
votes
1 answer

std::bad_alloc assigning a pointer from address of a reference

For whatever reason, I ended up with code that looked like this typedef std::vector Vector; void f(Vector const& v) { Vector const* p; p = &v; } This throws a bad_alloc exception at the point of assignment. Why? Does it matter that f…
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
-5
votes
2 answers

C++ List of Objects code throwing std::bad_alloc

I am writing the following a program and i get the std::bad_alloc exception. class A{ public: int arr[5000]; A() { for ( int i = 0; i < 5000; i++ ) { arr[i] = 0; } } }; int main() { int cnt=0; …
Codefather
  • 27
  • 1
  • 9
1 2 3
17
18