Follow Us
Contribute
  • Register

Std::bad alloc

0 votes
  1. This is one of the most common error massages for C++. It is usually appeared when Standard Template Libraries are used such as vectors and lists. Especially vectors consume a lot of memory, which may be the cause of this error message.
  2. Another cause is wrong implementation of arrays. Don't be surprised if you have a bug on line 10, and you encounter the error message on line 20. This is a very common thing to happen in C++.
requested 1 year ago by errorbase (169,970 points)
edited 1 year ago by onur

1 Solution

0 votes

This error is hard to solve and it may appear in many cases, so improve the article by adding as many cases as possible.

  1. The solution for this problem is specific to each implementation, but there are some common mistakes to avoid. For example, the elements of a vector must be filled with the pointers, not the objects themselves. Detailed solution manual about this error message will be given later.
  2. Avoid exceeding arrays. Don't do this:

     int *x;
     int a = 0,b = 0,c =0;
     x = new int[1];
     x[0] = a;
     x[1] = b;
     x[2] = c;

This code works, but if you continue to allocate memory, you may encounter this error message in the future.
 

solved 1 year ago by errorbase (169,970 points)
edited 1 year ago by onur

Related errors

0 votes
0 solutions
requested 1 month ago by Rahimi Raimi (120 points)
+1 vote
1 solution
0 votes
1 solution