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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <stdlib.h>
#include <stdio.h>
float ***mat3dyn (int nlines, int nrows, int depth)
{
int i, j;
float ***mat3 = malloc (nlines * sizeof *mat3); /* -ed- */
if (mat3 == NULL)
{
printf ("Error in matrix allocation (lines).\n");
exit (EXIT_FAILURE); /* -ed- */
}
for (i = 0; i < nlines; i++) /* -ed- etait nrows */
{
mat3[i] = malloc (nrows * sizeof *mat3[i]); /* -ed- */
if (mat3[i] == NULL)
{
printf ("Error in matrix allocation (rows).\n");
exit (EXIT_FAILURE); /* -ed- */
}
}
for (i = 0; i < nlines; i++)
for (j = 0; j < nrows; j++)
{
mat3[i][j] = calloc (depth, sizeof *mat3[i][j]); /* -ed- */
if (mat3[i][j] == NULL)
{
printf ("Error in matrix allocation (depth).\n");
exit (EXIT_FAILURE); /* -ed- */
}
}
return mat3;
}
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;
}
int main (void)
{
int nlines = 4;
int nrows = 3;
int depth = 2;
float ***m3 = mat3dyn (nlines, nrows, depth);
if (m3 != NULL)
{
freemat3 (m3, nlines, nrows);
}
return 0;
} |
Partager