0

I'm trying to create a 2d Array class for different use cases where I know the size at compile time and that it won't change during runtime. IE setting up a grid for a battleships game.

current implementation works in both debug and release when using balanced 2d arrays such as 2 by 2, however when using unbalanced 2d arrays like 2 by 3 it will work in release but not in debug. I get expression: array subscript out of range when debugging

im using visual studio 2022 RC with std 20 enabled

header file

template <class T,unsigned int colSize,unsigned int rowSize>
  class TwoDArray{
  public:
  TwoDArray(){

    for (int i = 0; i < colSize; ++i) {
        for (int j = 0; j < rowSize; ++j) {
            matrix[i][j] = j;
            std::cout << matrix[i][j] << " ";
         }
         std::cout <<  "   " << i << std::endl;
    }

 }
  private:
  std::array<std::array<T, colSize>, rowSize> matrix;

}

in main

int main(){  

   auto p = TwoDArray<int,2 ,3>{};
   
   return 0;
}

releaseBuild for creating 2d array in class .png

debugBuild for creating 2d array in class

2
  • You mixed up colSize and rowSize in your for loops. The outer one should be rowSize Commented Nov 7, 2021 at 21:57
  • The debug build is being helpful and adding additional checks that you aren't going out of bounds of the array, the release build lets you do whatever you want and you get "lucky" that it doesn't crash Commented Nov 7, 2021 at 22:07

1 Answer 1

0

Mixing up the colSize and rowSize in the for loops was the problem. Good to know about what the release build allows you to do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.