简体   繁体   English

尝试写入矩阵后出现C ++分段错误

[英]C++ Segmentation Fault After When Trying to Write to Matrix

I have this 3D matrix I allocated as one block of memory, but when I try to write to the darn thing, it gives me a segmentation fault. 我已将此3D矩阵分配为一个内存块,但是当我尝试写入该死的东西时,它给了我分段错误。 The thing works fine for two dimensions, but for some reason, I'm having trouble with the third...I have no idea where the error is in the allocation. 这个东西在两个维度上都可以正常工作,但是由于某种原因,我在第三个维度上遇到了麻烦...我不知道错误在哪里。 It looks perfect to me. 对我来说看起来很完美。

Here's the code: 这是代码:

phi = new double**[xlength];
phi[0] = new double*[xlength*ylength];
phi[0][0] = new double[xlength*ylength*tlength];
for (int i=0;i<xlength;i++)
{
    phi[i] = phi[0] + ylength*i;
    for (int j=0;j<ylength;j++)
    {
        phi[i][j] = phi[i][0] + tlength*j;
    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。 (Yes, I want a 3D matrix) (是的,我想要一个3D矩阵)

Also, this is where I get the segmentation fault if it matters: 另外,这很重要,这是我得到细分错误的地方:

for (int i = 0; i < xlength; i++)
    {
        for (int j = 0; j < ylength; j++)
        {
            phi[i][j][1] = 0.1*(4.0*i*h-i*i*h*h)
            *(2.0*j*h-j*j*h*h);
        }
    }

This does work for two dimensions though! 但这确实适用于二维!

phi = new double*[xlength];
phi[0] = new double[xlength*ylength];
for (int i=0;i<xlength;i++)
{
    phi[i] = phi[0] + ylength*i;
}

You did not allocate other submatrixes like eg phi[1] or phi[0][1] 您没有分配其他子矩阵,例如phi[1]phi[0][1]

You need at least 你至少需要

phi = new double**[xlength];
for (int i=0; i<xlength; i++) { 
    phi[i] = new double* [ylength];
    for (int j=0; j<ylength; j++) { 
       phi[i][j] = new double [zlength];
       for (k=0; k<zlength; k++) phi[i][j][k] = 0.0;
    }
}

and you should consider using std::vector (or even, if in C++2011, std::array ), ie 并且您应该考虑使用std :: vector (甚至在C ++ 2011中是std :: array ),即

std::vector<std::vector<double> > phi;

and then with std::vector you'll need to phi.resize(xlength) and a loop to resize each subelement phi[i].resize(ylength) etc. 然后使用std::vector您需要phi.resize(xlength)和一个循环来调整每个子元素phi[i].resize(ylength)等的大小。


If you want to allocate all the memory at once, you could have 如果要一次分配所有内存,则可以

double* phi = new double[xlength*ylength*zlength]

but then you cannot use the phi[i][j][k] notation, so you should 但是您不能使用phi[i][j][k]表示法,因此您应该

#define inphi(I,J,K) phi[(I)*xlength*ylength+(J)*xlength+(K)]

and write inphi(i,j,k) instead of phi[i][j][k] 并用inphi(i,j,k)代替phi[i][j][k]


Your second code does not work: it is undefined behavior (it don't crash because you are lucky, it could crash on other systems....), just some memory leak which don't crash yet (but could crash later, perhaps even by re-running the program again). 您的第二个代码不起作用:这是未定义的行为 (不会崩溃,因为您很幸运,它可能会在其他系统上崩溃...。),只是一些尚未发生崩溃的内存泄漏(但稍后可能会崩溃,甚至可以重新运行该程序)。 Use a memory leakage detector like valgrind 使用内存泄漏检测器(如valgrind)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM