Search code examples
c++dynamic-memory-allocation

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. 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)

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;
}

Solution

  • You did not allocate other submatrixes like e.g. phi[1] or 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), i.e.

    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.


    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

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

    and write inphi(i,j,k) instead of 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