Error on large dimension value - Run-Time Check Failure #2 - Stack around the variable 'mat' was corrupted

-1

I am getting following error:

"Run-Time Check Failure #2 - Stack around the variable 'mat' was corrupted" after giving result in console.

However, What I observed that, CreateMatrix function throws Access violation.. for larger matrix dimensions. For e.g. worked for 5x7 and did NOT work for 50 x 70. How ?

Program is just to create Matrix (initialize) and Set + print Matrix element.

Also, the caveat in problem is that I've been asked NOT to use anything like "Matrix* mat.." inside main(). Otherwise, solution is straight-forward.

I hope, you get my problem.

More detailed code:

struct Matrix
{
    int width;
int height;
int* data;
};
typedef struct Matrix Matrix;

int main()
{
    Matrix mat, *matP;

    //1. Things that Works...
    matP = CreateMatrix(&mat,700,500);         
    SetValue(matP,500,600,-295);
    int val=GetValue(matP,500,600);
    printf("%d\n",val); 


    //2. Things that does NOT work... !!!
    CreateMatrix(&mat,700,500); // this is C-style "Call-By-Reference" CreateMatrix2()
    SetValue(&mat,500,600,-295); // ERROR in this function, while setting matrix element
    val=GetValue(&mat,500,600);

    printf("%d\n",val); 
}


void SetValue(Matrix* mat,int row,int col,int value)
{
    *(mat[(mat->width*(row-1)) + (col-1)].data) = value;// <------ ERROR here

    // Unhandled exception at... Access violation writing location 0x000001f4
}

Matrix * CreateMatrix(Matrix *mat,int width,int height)
{        
    // mat = (Matrix*)malloc(width*height*(sizeof(Matrix))); // As told by Salgar
    mat->height = height;
    mat->width = width;

    for(int i=0; i < height; i++ )
    {
        for(int j=0; j < width; j++ )
        {
            mat[width*i + j].width = width;
            mat[width*i + j].height = height;
            mat[width*i + j].data = (int*)malloc(sizeof(int));
        }
    }
}
c
pointers
memory
memory-management
access-violation
asked on Stack Overflow Jun 11, 2013 by bharat.chandak100 • edited Jun 12, 2013 by bharat.chandak100

3 Answers

3

When you assign to mat on this line:

 mat = (Matrix*)malloc(width*height*(sizeof(Matrix)));

It is changing the Locally scoped copy of mat And hence nothing is happening to the mat you have declared on the stack in the calling function.

You need to pass a pointer to a pointer and not declare one on the stack, as you can't overwrite that with something you're declaring on the heap.

Matrix* mat;
CreateMatrix(&mat,700,500);

Matrix * CreateMatrix(Matrix **mat,int width,int height)
{
   ...
   *mat = malloc(etc)
   ...
}
answered on Stack Overflow Jun 11, 2013 by Salgar
0

You need to reference it correctly,
Instead of: (mat[(mat->width(row-1)) + (col-1)].data) = value;// <------ ERROR here

mat->data[(mat->width*(row-1)) + (col-1)] = value;// <------ NO ERROR here

Also, your CreateMatrix function is totally messed up

Matrix * CreateMatrix(Matrix *mat,int width,int height)
{        
    // mat = (Matrix*)malloc(width*height*(sizeof(Matrix))); // As told by Salgar
    mat->height = height;
    mat->width = width;

    for(int i=0; i < height; i++ )
    {
        for(int j=0; j < width; j++ )
        {
            mat[width*i + j].width = width;
            mat[width*i + j].height = height;
            mat[width*i + j].data = (int*)malloc(sizeof(int));
        }
    }
}

Use something like this instead:

Matrix * CreateMatrix(Matrix *mat,int width,int height)
{        
    mat->height = height;
    mat->width = width;
    mat->data = (int*) malloc(sizeof(int) * height * width);
}
answered on Stack Overflow Jun 12, 2013 by Pratyush Rathore • edited Jun 12, 2013 by Pratyush Rathore
0
struct Matrix
{
int width;
int height;
int* data;
};
typedef struct Matrix Matrix;

int main()
{
Matrix mat, *matP;

//2. Things that does NOT work... !!!
CreateMatrix(&mat,700,500); // this is C-style "Call-By-Reference" CreateMatrix2()
SetValue(&mat,500,600,-295); // ERROR in this function, while setting matrix element
val=GetValue(&mat,500,600);

printf("%d\n",val); 

free(mat->data);
} 


void SetValue(Matrix* mat,int row,int col,int value)
{
*(mat[(mat->width*(row-1)) + (col-1)].data) = value;// <------ ERROR here

// Unhandled exception at... Access violation writing location 0x000001f4
}

void CreateMatrix(Matrix *mat,int width,int height)
{        
mat->height = height;
mat->width = width;
mat->data = (int*)malloc(width*height*sizeof(int));
}

If I understand the restriction you have is, not to use pointers in main function, So we declare a local variable and then pass its pointer, which gets the data pointer and width and height assigned to it.

answered on Stack Overflow Jun 12, 2013 by user2477083

User contributions licensed under CC BY-SA 3.0