段错误C++程序
Segmentation fault C++ Program
我想写一个程序构造一个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++;
}
您的代码有待解决的问题:
可变长度数组 (VLA) 不是 C++ 标准的一部分。因此,使用如下语法:
int n = 10; // non-const value
int incorrect_array[n][n]; // invalid
编译时必须知道数组大小。
由于您在问题中提供的数组大小尚不清楚,堆栈溢出可能是段错误的另一个原因,以防您将数组大小溢出一个巨大的数字。
相同代码的修订版:
#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
大家有什么不明白的可以在评论中进一步提问
I want to write a program to construct a n*n matrix and fill it with 1 to n^2
您可以使用嵌套的标准容器或编写您自己的 user-defined class 矩阵数据结构模型。另请注意,有大量线性代数库提供精心设计和测试的矩阵 classes.
如果您决定实施一个,这可能是一个基本的起点
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.
// ...
};
有了它,您可以通过多种方式完成任务
// Using a nested loop
Matrix a(n, n);
for (size_t i{}, k{}; i < a.n_rows(); ++i) {
for (size_t j{}; j < a.n_columns(); ++j) {
a(i, j) = ++k;
}
}
// Using std::iota from <numeric> header
Matrix b(n, n);
std::iota(b.begin(), b.end(), 1);
I get a segmentation fault (core dumped). I have no clue why this happens.
那些行
int n;
cin >> n;
int array[n][n];
声明一个不符合标准的容器的可变长度数组。一些编译器提供这些作为扩展。特别是,gcc 还具有以下 documentation(强调我的)。
6.20 Arrays of Variable Length
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.
注意 automatic 术语的使用。
在评论中,您说您正在测试大小为 2000 的程序,这对您的环境来说可能太大了。参见例如why is stack memory size so limited?.
我想写一个程序构造一个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++;
}
您的代码有待解决的问题:
可变长度数组 (VLA) 不是 C++ 标准的一部分。因此,使用如下语法:
int n = 10; // non-const value int incorrect_array[n][n]; // invalid
编译时必须知道数组大小。
由于您在问题中提供的数组大小尚不清楚,堆栈溢出可能是段错误的另一个原因,以防您将数组大小溢出一个巨大的数字。
相同代码的修订版:
#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
大家有什么不明白的可以在评论中进一步提问
I want to write a program to construct a n*n matrix and fill it with 1 to n^2
您可以使用嵌套的标准容器或编写您自己的 user-defined class 矩阵数据结构模型。另请注意,有大量线性代数库提供精心设计和测试的矩阵 classes.
如果您决定实施一个,这可能是一个基本的起点
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.
// ...
};
有了它,您可以通过多种方式完成任务
// Using a nested loop
Matrix a(n, n);
for (size_t i{}, k{}; i < a.n_rows(); ++i) {
for (size_t j{}; j < a.n_columns(); ++j) {
a(i, j) = ++k;
}
}
// Using std::iota from <numeric> header
Matrix b(n, n);
std::iota(b.begin(), b.end(), 1);
I get a segmentation fault (core dumped). I have no clue why this happens.
那些行
int n;
cin >> n;
int array[n][n];
声明一个不符合标准的容器的可变长度数组。一些编译器提供这些作为扩展。特别是,gcc 还具有以下 documentation(强调我的)。
6.20 Arrays of Variable Length
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.
注意 automatic 术语的使用。
在评论中,您说您正在测试大小为 2000 的程序,这对您的环境来说可能太大了。参见例如why is stack memory size so limited?.