Hi,

I'm a new member who is fairly new to C++ and need a little help getting some bugs out of an assigned matrix project.

I am running VC++ 6.0 and have no compiler errors. Also, I have managed to run the program successfully for small matrices. My probelm occurs for large uneven matrices where I end up getting a segmentation fault. Using my debugger I isolated the most likely cause to my third constructor shown here:

Matrix::Matrix(int row, int col, string data)
{
    m_Rows = row;
    m_Cols = col;

    int i=0, j=0;

    mat_Data = new int *[m_Rows];
    for(int q=0; q<m_Rows; ++q)
        mat_Data[q] = new int [m_Cols];

    for(int k=0; k<data.size(); ++k){
        if(data[k] == '1'){
            mat_Data[i][j] = 1; //trouble with this assignment
            ++j;
            if(j >= m_Rows){
                j=0;
                ++i;
            }
        }
        else if(data[k] == '0'){
            mat_Data[i][j] = 0;
            ++j;
            if(j >= m_Rows){
                j=0;
                ++i;
            }
        }
        else if(isspace(data[k])|| data[k] == ',')
            continue;

        else
            throw MatrixError();
    }

}

Note that this project is restricted to entries of matrices- integer modulo 2.
I am not sure why the compiler is dumping the the program for only certain types of matrices.

I would appreciated any help I could get from one of you C++ experts.

Thanks

Edited 3 Years Ago by happygeek: fixed formatting

Comments
Use code tags.

>My probelm occurs for large uneven matrices where I end up getting a segmentation fault.
By default, Visual C++ 6 doesn't throw an exception when new fails, it returns a null pointer just like malloc. Your problem looks suspiciously like one of the allocations is failing and then you try to dereference a null pointer. Your first order of business should be adding proper error handling code so that you can have a better idea of where the source of the problem is rather than the symptom.

Sorry,

Thanks for the help, but I don't exactly understand what you're saying for my new statement. I've tested the program under different compilers in linux and that doesn't seem to be the problem.

What is it exactly about: mat_Data[j] = 1 that could cause a seg. fault for certain matrices when I allocated memory and defined int** mat_Data in my class.

Also, how could I better implement error-checking procedures in my program.

Thanks

>What is it exactly about: mat_Data[j] = 1 that could cause a seg.
A segmentation fault is caused when you access memory outside of your address space. Subscripting an array is effectively a pointer dereference, and if the indices are beyond the bounds of the array then you'll probably get a seg fault. If one of your allocations fails then you're working with a null pointer, which can't be dereferenced, which will crash and burn if you try, which is what appears to be happening.

>I've tested the program under different compilers
Different compilers may be new enough to throw an exception rather than return a null pointer.

>how could I better implement error-checking procedures in my program.

mat_Data = new int *[m_Rows];
if ( mat_Data == NULL ) {
  // Error! There's not a whole lot you can do though, terminate gracefully
}

Repeat that process for every use of new and you'll have gone a long way toward improving your error handling.

This article has been dead for over six months. Start a new discussion instead.