Vector going out of bounds without giving error when triying to display

When i run this program, i create a vector succesfully, but in the next loop the program stops working without giving an error. when i trie to cout one element of a vector by means of: cout<<Array[0][0][0]; or cout<<Array[0]; it also doest work.

Some backround:
I try to create a 3dimensional vector for a sudoku puzzel. I assign every element of the 2d puzzel with a vector containing all possible enties (1,2,3,4,5,6,7,8,9).

This is my code:

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
//create dynamic vector 3d
const int SizeOfSudoku = 3;
const int rows = pow(SizeOfSudoku,2);
const int columns = pow(SizeOfSudoku,2);
const int possibles = pow(SizeOfSudoku,2);

vector< vector < vector<int> > > Array(rows*columns*possibles);
vector<vector<int> > Array1(columns*possibles);
vector<int> Array2(possibles);
for(int i=0; i<rows; i++){
__Array.push_back( Array1 );
__for(int j=0; j<columns; j++){
____Array1.push_back( Array2 );
____for(int k=1; k<possibles; k++){
______Array2.push_back( k );
____}
__}
}

cout<<"created vector"<<endl;
cout<<Array.size()<<endl;


//display sudoku
for(int i=0; i<9; i++){
__cout<<i<<endl<<endl;
__for(int j=0; j<9; j++){
____cout<<j<<endl;
____for(int k=0; k<9; k++){
______cout << Array[i][j][k] << " ";
____}
__}
}
cout<<endl<<endl<<"displayed vector"<<endl;


cout << "Hello world!" << endl;
return 0;
}



The program outputs these correctly:
cout<<"created vector"<<endl;
cout<<Array.size()<<endl;

The program runs into problams at:
cout << Array[i][j][k] << " ";


Please help me, i am desperate.

Btw. I gave the underscores to display the code in a easier to read way. Here is the code for copying purposes:

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
//create dynamic vector 3d
const int SizeOfSudoku = 3;
const int rows = pow(SizeOfSudoku,2);
const int columns = pow(SizeOfSudoku,2);
const int possibles = pow(SizeOfSudoku,2);

vector< vector < vector<int> > > Array(rows*columns*possibles);
vector<vector<int> > Array1(columns*possibles);
vector<int> Array2(possibles);
for(int i=0; i<rows; i++){
Array.push_back( Array1 );
for(int j=0; j<columns; j++){
Array1.push_back( Array2 );
for(int k=1; k<possibles+1; k++){
Array2.push_back( k );
}
}
}

cout<<"created vector"<<endl;
cout<<Array.size()<<endl;
cout<<Array[0][0][0]<endl;


//display sudoku
for(int i=0; i<9; i++){
cout<<i<<endl<<endl;
for(int j=0; j<9; j++){
cout<<j<<endl;
for(int k=0; k<9; k++){
cout << Array[i][j][k] << " ";
}
}
}
cout<<endl<<endl<<"displayed vector"<<endl;


cout << "Hello world!" << endl;
return 0;
}



Last edited on
Please use code tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
   //create dynamic vector 3d
   const int SizeOfSudoku = 3;
   const int rows = SizeOfSudoku * SizeOfSudoku;
   const int columns = rows;
   const int possibles = rows;
   
   vector< vector < vector<int> > > Array(rows);
   vector< vector<int> > Array1(columns);
   vector<int> Array2(possibles);

   for( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < columns; j++ )
      {
         for ( int k = 0; k < possibles; k++ )
         {
            Array2[k] = k + 1;
         }
         Array1[j] = Array2;
      }
      Array[i] = Array1;
   }
   
   cout <<"created vector" << endl;
   cout << Array.size() << endl;
   cout << Array[0][0][0] << endl;
   
   //display sudoku
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < columns; j++ )
      {
         for ( int k = 0; k < possibles; k++ )
         {
            cout << i << " " << j << " " << k << " " << Array[i][j][k] << '\n';
         }
      }
   }
}
To improve readability use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/

1
2
3
vector< vector < vector<int> > > Array(rows*columns*possibles);
vector<vector<int> > Array1(columns*possibles);
vector<int> Array2(possibles);
Array1/Array2 are unrelated to Array. So changing them will not have any effect regarding Array

To create a 3-d array you might do this with the latest C++ Version:

std::vector<std::vector<std::vector<int>>> Array{rows, {columns, {possibles}}}; // No push_back() necessary

See (fill consructor):

http://www.cplusplus.com/reference/vector/vector/vector/
Topic archived. No new replies allowed.