Matrice avec trois indices
Bonjour,
Je souhaite utiliser des matrices avec trois indices, voici comment je leur alloue de la memoire:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
float ***mat3dyn(int nlines, int nrows, int depth)
{
int i,j;
float ***mat3;
mat3=malloc(nlines*sizeof(float **));
if(mat3==NULL)
{
printf("Error in matrix allocation (lines).\n");
exit(0);
}
for(i=0;i<nrows;i++)
{
mat3[i]=malloc(nrows*sizeof(float *));
if(mat3[i]==NULL)
{
printf("Error in matrix allocation (rows).\n");
exit(0);
}
}
for(i=0;i<nlines;i++)
for(j=0;j<nrows;j++)
{
mat3[i][j]=calloc(depth,sizeof(float));
if(mat3[i][j]==NULL)
{
printf("Error in matrix allocation (depth).\n");
exit(0);
}
}
return(mat3);
} |
Voici comment je libere la dite memoire:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void freemat3(float ***mat3, int nlines, int nrows)
{
int i,j;
for(i=0; i<nlines; i++)
for(j=0;j<nrows; j++)
{
free(mat3[i][j]);
mat3[i][j]=NULL;
}
for(i=0;i<nlines;i++)
{
free(mat3[i]);
mat3[i]=NULL;
}
free(mat3);
mat3=NULL;
} |
J'aimerais savoir si c'est bon, mon programme plante et je me demande si cela ne vient pas de la.
Merci.