std::bad_alloc

Hi, I come across a task while trying to improve my C++ skills. It compiled fine and gives me the correct result. But when executing with a large data, it gives me std::bad_alloc, which I think is from a memory leak? So any idea how to overcome or what I can do?

This is the function I trying to call (which no modification is allowed)
1
2
3
4
5
6
7
8
9
10
11
std::vector<int>* CreateRandomizedIntVector()
{
    std::vector<int> *result = new std::vector<int>();

    for (int i = 0, size = std::rand(); i < size; i++)
    {
        result->push_back(std::rand());
    }

    return result;
}


The task is to call the function which will create some vector<int> and I need to sum them up and determine the total is an odd or even number. Below is my code: I'm stuck on how to fix that memory leak. Thanks for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <cstdlib>

int main ()
{
    bool sumIsOdd;
    int sum = 0;
    std::vector<int>* test = CreateRandomizedIntVector();
    
    while (!test->empty())
    {
        sum += test->back();
        test->pop_back(); 
    }
    
    delete[] test;

    if (sum % 2 == 0) { sumIsOdd = false; }
    else { sumIsOdd = true; }
    
    if (sumIsOdd) { std::cout << "Sum is odd.\n"; }
    else { std::cout << "Sum is even.\n"; }
}
Last edited on
bad_alloc means that a memory allocation failed. It can happen if you allocate too much memory.

Note that the range of rand() varies widely between different implementations. The maximum value of rand() can be as low as 32767 on some implementations and as high as 2147483647 on others.

32767 ints requires 128 KB of memory which is not much of a problem for a modern computer.
2147483647 ints requires 8 GB which is more likely to be a problem.
Last edited on
Thanks for the answer. But the rand() function is being called in CreateRandomizedIntVector function which I not suppose to modify.

My own code didn't use it, so I can assume its the line where I called
std::vector<int>* test = CreateRandomizedIntVector();
Is there a way to overcome this without changing the CreateRandomizedIntVector function? Don't think declare or assign a larger memory allocation will help? Does that mean I have to do something with the pointer part?
That line isn't really giving you trouble. I ran your code in a debugger and I found that the program crashes at the last iteration of the while loop because test->empty() incorrectly returns true.

At least in Visual Studio, whaat makes it crash is a write access violation.


EDIT: I'm an idiot, I didn't notice you used delete[] instead of delete (notice the missing operator []).
What you're trying to accomplish is deleting a pointer to a vector, not a pointer to an array of vectors. The two things are actually very different.
Last edited on
Thanks so much for the help. I think I have some idea on what I can do. Will let you know how it goes and hopefully I can fix it.
You literally only need to replace
delete[] test
with
delete test

You only use delete[] when you want to deallocate c-style arrays or double pointer (e.g. An array of vectors) . When you're deallocating a pointer to an object (even a container) you use delete.
I try that and it still gives me bad_alloc error. I took a snapshot, the link at
https://ibb.co/hPppa7
Could its be a different compiler I used?
Last edited on
the rand() function is being called in CreateRandomizedIntVector function which I not suppose to modify.

There are so many things wrong with such as small function it's actually kind of sad.

Is there a way to overcome this without changing the CreateRandomizedIntVector function?

Since that function calls rand() to decide how large a vector to make, you can call srand() before calling it, with a seed chosen so that the next std::rand() will return something small.

Just go over a few seeds and see what the first rand comes out as, and pick the first one that's under, say, 10000:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <climits>
#include <cstdlib>
int main()
{

    for(unsigned int n = 0; n != UINT_MAX; ++n) {
      std::srand(n);
      int x = std::rand();
      if(x < 10000) {
          std::cout << "when seeded with " << n
                    << " this rand() returns " << x << '\n';
          break;
      }
    }
}

running this with gcc/libstdc++ I get (https://wandbox.org/permlink/AfYyWG3S1T9zJUNH)
when seeded with 37325 this rand() returns 2648

with Visual Studio I get (http://rextester.com/IJKRJW18058)
when seeded with 0 this rand() returns 38


So, depending where that has to run, call the right srand() and your memory will be safe.
Last edited on
Thank for the reply. Just so we on the same page, I run some srand() with a seed before calling the CreateRandomizedIntVector().

This will help me get some idea on how large the rand() will be, hence I can declare the vector size to use before calling the method. Am I getting this correct? But then, why are we picking the rand value to be less than 10000? to get a max value it will assigns, shouldn't we be finding greater than 10000?

Sorry, still trying to wrap my mind around this.
why are we picking the rand value to be less than 10000?

The next time rand() will run, it will give you that value.
So, your code becomes
1
2
3
4
5
6
7
int main ()
{
    bool sumIsOdd;
    int sum = 0;
    srand(37325); // if using GCC
    std::vector<int>* test = CreateRandomizedIntVector();
// as you were 

now you know exactly how large a vector CreateRandomizedIntVector creates, and you know it's not 1804289383 or something else outrageous


The CreateRandomizedIntVector() has obviously been written with the assumption that rand() has a very limited range, so the problem that you are facing is not what the original author intended. The solution that Cubbi proposed is a hack that you should never need to do in real code. You can of course try to solve it anyway as a fun exercise but in general I would say that such a buggy function should either be fixed, or not be used at all.
Its all good now. Thanks so much for all the help and advice. I try not to write code like CreateRandomizedIntVector().
Topic archived. No new replies allowed.