Problème allocation dynamique d'un tableau
Résolu
KiitKaate
Messages postés
4
Date d'inscription
Statut
Membre
Dernière intervention
-
KiitKaate Messages postés 4 Date d'inscription Statut Membre Dernière intervention -
KiitKaate Messages postés 4 Date d'inscription Statut Membre Dernière intervention -
A voir également:
- Problème allocation dynamique d'un tableau
- Tableau croisé dynamique - Guide
- Tableau word - Guide
- Exemple tableau croisé dynamique télécharger - Télécharger - Tableur
- Trier un tableau excel - Guide
- Tableau ascii - Guide
3 réponses
Il faut free(g) également
edit: correction du code
#include <stdio.h> #include <stdlib.h> typedef struct s_grille { int nbl; int nbc; int **cellules; } Grille; void alloue_grille (int l, int c, Grille *g); void libere_grille (Grille* g); int main(){ int i,j,c=0; int line = 30, col = 50 ; Grille *g = malloc(sizeof(Grille)); alloue_grille(line,col,g); //remplissage for(i = 0; i < line; i++){ for( j = 0; j < col ; j++){ g->cellules[j][i] = c++; } } //affichage for(i = 0; i < line; i++){ for( j = 0; j < col ; j++){ printf("%d ", g->cellules[j][i]); } printf("\n"); } libere_grille(g); return 0; } void alloue_grille (int l, int c, Grille *g){ int i; g->nbl=l; g->nbc=c; g->cellules = (int**)malloc( c * sizeof(int*)); for(i=0;i<c;i++) g->cellules[i] = (int*)calloc(l,sizeof(int*)); } void libere_grille (Grille* g){ int i; for(i=0;i<g->nbc;i++) free(g->cellules[i]); free(g->cellules); free(g); }
==7077== HEAP SUMMARY:
==7077== in use at exit: 0 bytes in 0 blocks
==7077== total heap usage: 53 allocs, 53 frees, 13,440 bytes allocated
==7077==
==7077== All heap blocks were freed -- no leaks are possible
==7077==
edit: correction du code
Sur valgrind j'obtiens ceci:
Conditional jump or move depends on uninitialised value(s)
==2709== at 0x4C2DB3C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2709== by 0x40146F: alloue_grille (grille.c:42) //erreur au niveau du 1er malloc
==2709== by 0x4012FD: init_grille_from_file (grille.c:18)
==2709== by 0x400899: main (main.c:21)
in use at exit: 552 bytes in 1 blocks
==3101== total heap usage: 2 allocs, 1 frees, 4,648 bytes allocated