Using dynamic multi-dimensional arrays in c++

后端 未结 6 523
礼貌的吻别
礼貌的吻别 2020-12-21 01:24

I am making a C++ program that checks if given aray is a latin square. I need to use a dynamic multi-dimensional array that stores given latin square. But I cant pass the ar

6条回答
  •  感情败类
    2020-12-21 01:49

    You made yourself a pointer pointer that can be used as a matrix, allocated one row for it, then proceeded to act like you'd allocated an entire n*n matrix. You will indeed get a segfault if you run that.

    You need to allocate enough space for n*n elements, not just n of them.

    A less error-prone solution might be to use a std::vector of std::vectors.

提交回复
热议问题