C++ 分段故障C++;节目

C++ 分段故障C++;节目,c++,segmentation-fault,C++,Segmentation Fault,我想写一个程序来构造一个n*n矩阵,并用1到n^2填充它。但我‌ 获取分段错误(堆芯转储) 我‌ 我不知道为什么会这样。任何帮助都将不胜感激 int array[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { array[i][j] = t; t++; } int数组[n][n]; 对于(int i=0;i数组允许在ISO C99 中,作为扩展

我想写一个程序来构造一个n*n矩阵,并用1到n^2填充它。但我‌ 获取分段错误(堆芯转储)

我‌ 我不知道为什么会这样。任何帮助都将不胜感激

  int array[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      array[i][j] = t;
      t++;
    }
 
int数组[n][n];
对于(int i=0;i
代码中需要解决的一些问题:

#include <iostream>
#include <vector>

// Using this syntax just to avoid 'std::' prefix everywhere
// in this program.
using namespace std;

int main(void) {
  // Initializing multidimensional vector which will work as an array
  vector<vector<int>> multiDim;
  int size;
  int a = 1;

  cout << "The NxN size: ";
  cin >> size;

  // Providing a size of the multi-dim array
  multiDim.resize(size, vector<int>(size));

  // Initializing 1 to n^2
  for (int i{}; i < size; i++)
    for (int j{}; j < size; j++)
      multiDim[i][j] = a++;
  
  // Uncomment the next lines 5 lines to see output preview
  // for (const auto& it : multiDim) {
  //   for (const auto& subIt : it)
  //     cout << subIt << '\t';
  //   cout << endl;
  // }

  return 0;
}
The NxN size: 5
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
>可变长度数组(VLAS)不是C++标准的一部分,因此,使用类似于

的语法
int n = 10; // non-const value
int incorrect_array[n][n]; // invalid
数组大小必须在编译时已知

  • 由于您在问题中提供的数组的大小尚不清楚,如果您的数组大小溢出了一个巨大的数字,堆栈溢出可能是SEGFULT的另一个原因


  • 同一代码的修订版:

    #include <iostream>
    #include <vector>
    
    // Using this syntax just to avoid 'std::' prefix everywhere
    // in this program.
    using namespace std;
    
    int main(void) {
      // Initializing multidimensional vector which will work as an array
      vector<vector<int>> multiDim;
      int size;
      int a = 1;
    
      cout << "The NxN size: ";
      cin >> size;
    
      // Providing a size of the multi-dim array
      multiDim.resize(size, vector<int>(size));
    
      // Initializing 1 to n^2
      for (int i{}; i < size; i++)
        for (int j{}; j < size; j++)
          multiDim[i][j] = a++;
      
      // Uncomment the next lines 5 lines to see output preview
      // for (const auto& it : multiDim) {
      //   for (const auto& subIt : it)
      //     cout << subIt << '\t';
      //   cout << endl;
      // }
    
      return 0;
    }
    
    The NxN size: 5
    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
    
    如果您有任何疑问,可以在评论中进一步询问

    我想写一个程序来构造一个n*n矩阵,并用1到n^2填充它

    您可以使用嵌套的标准容器,也可以编写自己的用户定义类来模拟矩阵数据结构。还请注意,有大量线性代数库提供了设计良好且经过测试的矩阵类

    如果您决定实施一个,这可能是一个基本的起点

    class Matrix
    {
        size_t rows_, cols_;
        std::vector<int> m_;   // Note that this is ONE vector
    public:
        Matrix() = default;
        Matrix(size_t r, size_t c) 
            : rows_{r}, cols_{c}, m_(r * c)
        {}
        size_t n_rows() const noexcept { 
            return rows_;
        }
        size_t n_columns() const noexcept {
            return cols_;
        }
    
        // I prefer to overload operator() for matrices, instead of operator[]. We need a little
        // formula to calculate the 1D index, given the 2D indices.
        auto operator() (size_t r, size_t c) const {
            return m_[r * cols_ + c];
        }
        auto& operator() (size_t r, size_t c) {
            return m_[r * cols_ + c];
        }
    
        auto begin() {
            return m_.begin();
        }
        auto end() {
            return m_.end();
        }
        // You may want the const version and cbegin(), cend(), too.
        // ...
    };
    
    声明一个可变长度数组,它不是一个标准兼容的容器。一些编译器提供theese作为扩展。特别是,gcc还具有以下功能(我的重点)

    6.20可变长度数组

    变量长度<强>自动< /Stord>数组允许在ISO <强> C99 <强>中,作为扩展GCC接受它们在C90模式和C++中。这些数组声明为任何其他的<强>自动< /强>数组,但长度不是常数表达式。当包含声明的块作用域退出时,ted

    注意这个词的用法


    在注释中,你说你正在测试的程序大小为2000,这可能对你的环境来说太大了。参见,

    它可能是堆栈溢出。你对代码> N< /代码>有什么价值?VLA不是标准C++。你曾经尝试过什么代码> N<代码>?是在堆栈上分配的,较大的n可能会导致堆栈溢出。可能的解决方案:当
    n
    的大小较大时,这可能是由于堆栈溢出引起的。假设您将
    n=2000
    ,您正在分配
    n*n
    的内存空间,因此
    4000000*sizeof(int)
    正在分配。@无工作运行时间取决于系统性能。如果我运行两次或三次,在我的机器上可能会有所不同。@无工作是可能的。很明显,你的导师对同一程序有不同的编码方法,一个程序不仅有一个可实现的解决方案,而且可能会有所不同。这是最好的代码我会为这个问题写信的。