Src: add checks, free memory (clean valgrind output)
This is (willingly) not reflected in the poly: implementation details are just distracting from the essential
This commit is contained in:
parent
8c86e255b0
commit
f6952d4800
1 changed files with 15 additions and 0 deletions
15
src/liste.c
15
src/liste.c
|
@ -7,6 +7,15 @@ struct liste {
|
|||
};
|
||||
typedef struct liste liste_t;
|
||||
|
||||
void free_liste(liste_t* liste) {
|
||||
liste_t* cur = liste;
|
||||
while(cur != NULL) {
|
||||
liste_t* prev = cur;
|
||||
cur = cur->suivant;
|
||||
free(prev);
|
||||
}
|
||||
}
|
||||
|
||||
// Alloue une nouvelle cellule de
|
||||
// valeur `val`
|
||||
liste_t* alloc_cell(int val) {
|
||||
|
@ -26,9 +35,15 @@ void afficher_liste(liste_t* liste, int taille) {
|
|||
}
|
||||
}
|
||||
int main(int argc, char** argv) {
|
||||
if(argc < 2) {
|
||||
fprintf(stderr, "Missing argument: number of elements to print\n");
|
||||
exit(1);
|
||||
}
|
||||
int n = atoi(argv[1]); // premier argument
|
||||
liste_t* tete = alloc_cell(1);
|
||||
tete->suivant = alloc_cell(2);
|
||||
afficher_liste(tete, n);
|
||||
|
||||
free_liste(tete);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue