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
| double **creer_matrice (size_t lignes, size_t colonnes)
{
double **pp_matrice = NULL;
if (lignes > 0 && colonnes > 0)
{
pp_matrice = malloc(lignes * sizeof *pp_matrice);
if (pp_matrice != NULL)
{
size_t i;
int err_alloc = 0;
for (i = 0; err_alloc == 0 && i < lignes; ++i)
{
pp_matrice[i] = malloc(colonnes *sizeof *pp_matrice[i]);
}
if (err_alloc != 1)
{
/* initialisation de tous les membres à 0.0 */
size_t j;
for (i = 0; i < lignes; ++i)
{
for (j = 0; j < colonnes; ++j)
{
pp_matrice[i][j] = 0.0;
}
}
}
else
{
/* Erreur d'allocation: on fait le menage */
for (i--; i > 0; i--)
{
free(pp_matrice[i]);
}
free(pp_matrice), pp_matrice = NULL;
}
}
else
{
/* Erreur d'allocation: rien a faire. La fonction retournera NULL */
}
}
return pp_matrice;
} |