std::bad_alloc When using less memory ?




Base conversion example: 

If the number 17 was to be converted into base 5 

17/3 = 5R2
5/3= 1R2
2/3 = 0R1

The value in the new base = All the remainders in reverse order
In this case Remainders = 221
In reverse order = 122








Last edited on
Perhaps
1
2
        std::vector<int> tmp;
        tmp[i] = list[i][0];


Should be
 
        std::vector<int> tmp = list[i];


Or better yet, since it doesn't make a copy
 
        std::vector<int> &tmp = list[i];

1
2
3
for( int i = 0; i< list.size(); i++) {
  std::vector<int> tmp; // create empty vector
  tmp[i] = list[i][0]; // assign to element i, even though tmp has no elements 

Furthermore, you do read integer list[i][0] without checking. The list[i] could be an empty vector.

Do consider:
1
2
3
4
5
6
7
8
9
int main(){
    std::vector<std::vector<int>> list;
    generate_list( list , 6, 4 );
    for ( const auto& row : list ) {
        for ( auto x : row ) {
            std::cout << x << '\n';
        }
    }
}


Or (if you need to a copy):
1
2
3
4
5
6
7
8
9
10
int main(){
    std::vector<std::vector<int>> list;
    generate_list( list , 6, 4 );
    for ( size_t i = 0; i< list.size(); ++i ) {
        std::vector<int> row = list[i];
        for ( auto x : row ) {
            std::cout << x << '\n';
        }
    }
}

@Keskiverto Thank you for your post.

I have adjusted the main for this however when I switch out the 6 and the 4 I get an error saying:


terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


I read online and this is an issue which comes up when you have gone over the memory limit but the issue is :

The limit to how many numbers are generated is:
int possibilities = std::pow(symbols, length);

generate_list( list , 6, 4 ); generates 4^6 different numbers = 4096

However when you do generate_list( list , 4, 6 ); it generates 6^4 numbers = 1296

How is it giving a not enough memory error when it generates less numbers?
Last edited on
If I'm reading the code right, you're storing every possible number with base "symbols" and "length" digits. You're storing each digit individually.

Why??

If you need the digits of a base B number, it makes much more sense to compute them on the fly rather than store the digits of every possible number.
@dhayden


I need this for a programme I'm doing so it requires all of those numbers to be stored.

In addition to this, the base= length, digits = symbols .
Last edited on
Please repost your original code. The purpose of this site is to help people learn and no one can learn from this thread without the original code.

In addition to this, the base= length, digits = symbols .
That very confusing to me. If you check your code, you'll find that it's confusing to you too. :)
I think we have a classic XY problem here.

Previous threads for reference:
http://www.cplusplus.com/forum/beginner/250667/
http://www.cplusplus.com/forum/beginner/250765/#msg1104221
Last edited on
Topic archived. No new replies allowed.