In c++ a bad_alloc happen while assigning memory, or at least that was what I understood, but now I get this error while reading memory from an array. I create the array:
int * possible = new int[ suma ];
and then i got two for cycles, where I access the whole array:
for( int i = 0; i < n - 1; i++ ){
int rowWidth = (i - 1) < 0 ? 0 : permutationsNumber[i];
for( int j = 0; j < permutationsNumber[i]; j++ ){
possible[i * rowWidth + j ] = j;
}
}
for( int i = 0; i < n - 1; i++ ){
int rowWidth = (i - 1) < 0 ? 0 : permutationsNumber[i];
for( int j = 0; j < permutationsNumber[i]; j++ ){
std::cout << possible[i * rowWidth + j ] << "\n";
}
In the second for in the line std::cout << possible[i * rowWidth + j ] << "\n";
is where i got the error.
I compile the code on Windows, tried using 3 different machines with windows 8 and 7, using VS 2010, 2012 and 2013 and got the same error.
When I compile the code on Mac i got no error, same using compileonline
The variable values I didn't think as important are:
n = 4;
permutationsNumber = {4, 12, 24};
suma = 40;