thanks for your idea
I'm trying as you wrote, but I think, that I make mistake somewhere.
// I would like to alocate as 2D array
TMatrix alocation(unsigned int row, unsigned int col)
{
TMatrix matrix;
int *mat = malloc(row * col * sizeof(int));
if (mat == NULL)
{
matrix_error();
free(mat);
}
matrix.col = col;
matrix.row = row;
matrix.matrix = mat;
return matrix;
}
void freeMatrix(TMatrix *matrix)
{
free(matrix->matrix);
}
or I tried this:
TMatrix alocation(unsigned int row, unsigned int col)
{
int**array = malloc(row*sizeof(int*));
if(array == NULL)
mistake();
for (int i = 0; i < row; i++)
{
array[i] = malloc(col*sizeof(int));
if (array[i] == NULL)
{
for(int u = 0; u < i; u++)
free(array[u]);
free(array);
mistake();
break;
}
}
}
//dealocation
void freeMatrix(int)
{
for (int i=0; i<row; i++)
{
free(array[i]);
}
free(array);
}
I am agonized, I'm trying all night and I can't make it in order to be OK. I can try write something else...
Milla