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:
Théophile Bastian 2021-11-22 15:59:59 +01:00
parent 8c86e255b0
commit f6952d4800
1 changed files with 15 additions and 0 deletions

View File

@ -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;
}