问题描述
我在Linux中编写一些C ++代码,我已经声明了一些二维数组,如下:
double x [5000 ] [500],y [5000] [500],z [5000] [500];
在编译期间没有错误。当我执行它说分段错误。
Wen我将数组的大小从5000减少到50,程序运行良好。
如果您的程序看起来像这样...
int main(int,char **){
double x [5000] [500],y [5000] [500] 5000] [500];
// ...
return 0;
}
...那么你就溢出堆栈。修复此问题的最快方法是添加字词 static 。
**){
static double x [5000] [500],y [5000] [500],z [5000] [500];
// ...
return 0;
}
解决这个问题的第二个最快的方法是将声明移出函数:
double x [5000] [500],y [5000] [500],z [5000] [500]
int main(int,char **){
// ...
return 0;
}
解决这个问题的第三个最快的方法是在堆上分配内存:
int main(int,char **){
double ** x = new double * [5000];
double ** y = new double * [5000];
double ** z = new double * [5000];
for(size_t i = 0; i x [i] = new double [500];
y [i] = new double [500];
z [i] = new double [500];
}
// ...
for(size_t i = 5000; i> 0;){
delete [] z [ - i]
delete [] y [i];
delete [] x [i];
}
delete [] z;
delete [] y;
delete [] x;
return 0;
}
第四快的方法是使用std :: vector 。它在您的文件中的行数较少,但在编译单元中有更多的行,并且您必须为导出的向量类型考虑有意义的名称,或将它们添加到匿名命名空间中,以便它们不会污染全局命名空间:
#include< vector>
using std :: vector
namespace {
struct Y:public vector< double> {Y():vector< double>(500){}};
struct XY:public vector< Y> {XY():vector< Y>(5000){}};
}
int main(int,char **){
XY x,y,z;
// ...
return 0;
}
第五快的方法是在堆上分配它们,尺寸不太远离对象:
include< vector>
using namespace std;
namespace {
template< size_t N>
struct Y:public vector< double> {Y():vector< double>(N){}};
template< size_t N1,size_t N2>
struct XY:public vector< Y N2 > {XY():vector< Y N2 > (N1){}};
}
int main(int,char **){
XY< 5000,500> x,y,z;
XY< 500,50> mini_x,mini_y,mini_z;
// ...
return 0;
}
最有效的方法是将二维数组分配为一维数组,然后使用索引算术。
上述所有假设你有一些理由,一个好的或一个穷人,想制作自己的多维数组机制。如果没有理由,并希望再次使用多维数组,请考虑安装库:
-
with-STL方式是
使用。
I am writing some C++ code in Linux where I have declared a few 2D arrays like so:
double x[5000][500], y[5000][500], z[5000][500];
During compilation there is no error. When I execute it says "segmentation fault".
Wen I reduce the size of the array from 5000 to 50, the program runs fine. How can I protect myself against this problem?
If your program looks like this ...
int main(int, char **) {
double x[5000][500],y[5000][500],z[5000][500];
// ...
return 0;
}
... then you are overflowing the stack. The fastest way to fix this is to add the word static.
int main(int, char **) {
static double x[5000][500],y[5000][500],z[5000][500];
// ...
return 0;
}
The second fastest way to fix this is to move the declaration out of the function:
double x[5000][500],y[5000][500],z[5000][500];
int main(int, char **) {
// ...
return 0;
}
The third fastest way to fix this is to allocate the memory on the heap:
int main(int, char **) {
double **x = new double*[5000];
double **y = new double*[5000];
double **z = new double*[5000];
for (size_t i = 0; i < 5000; i++) {
x[i] = new double[500];
y[i] = new double[500];
z[i] = new double[500];
}
// ...
for (size_t i = 5000; i > 0; ) {
delete[] z[--i];
delete[] y[i];
delete[] x[i];
}
delete[] z;
delete[] y;
delete[] x;
return 0;
}
The fourth fastest way is to allocate them on the heap using std::vector. It is fewer lines in your file but more lines in the compilation unit, and you must either think of a meaningful name for your derived vector types or tuck them into an anonymous namespace so they won't pollute the global namespace:
#include <vector>
using std::vector
namespace {
struct Y : public vector<double> { Y() : vector<double>(500) {} };
struct XY : public vector<Y> { XY() : vector<Y>(5000) {} } ;
}
int main(int, char **) {
XY x, y, z;
// ...
return 0;
}
The fifth fastest way is to allocate them on the heap, but use templates so the dimensions are not so remote from the objects:
include <vector>
using namespace std;
namespace {
template <size_t N>
struct Y : public vector<double> { Y() : vector<double>(N) {} };
template <size_t N1, size_t N2>
struct XY : public vector< Y<N2> > { XY() : vector< Y<N2> > (N1) {} } ;
}
int main(int, char **) {
XY<5000,500> x, y, z;
XY<500,50> mini_x, mini_y, mini_z;
// ...
return 0;
}
The most performant way is to allocate the two-dimensional arrays as one-dimensional arrays, and then use index arithmetic.
All the above assumes that you have some reason, a good one or a poor one, for wanting to craft your own multidimensional array mechanism. If you have no reason, and expect to use multidimensional arrays again, strongly consider installing a library:
A plays-nicely-with-STL way is touse the Boost MultidimensionalArray.
A speed way is to use Blitz++.
这篇关于大二维阵列给出分割错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!