I am trying to create a matrix with dynamic proportions and to initialize it here is the code I am using to allocate memory and initialization:
int **matrix;
//mem allocation
matrix=(int*)malloc(sizeof(int*)*mat_w);
for (i=0;i<mat_w;i++)
matrix[i]=(int)malloc(sizeof(int)*mat_h);
//init
for (i=0;i<mat_w;i++)
for (j=0;j<mat_h;j++)
matrix[i][j]=0;
This , works fine , question is , if I try to create a matrix of type short - I get a segmentation error on the init first pass.
Is this a C language issue or am I doing something wrong?
Code for matrix of type short
:
short **matrix;
//mem allocation
matrix=(short*)malloc(sizeof(short*)*mat_w);
for (i=0;i<mat_w;i++)
matrix[i]=(short)malloc(sizeof(short)*mat_h);
//init
for (i=0;i<mat_w;i++)
for (j=0;j<mat_h;j++)
matrix[i][j]=0;
P.S.: I dropped safety checks , index variables and boundary declarations for clarity of code.
Thanks,
Alex