Bonsoir, en voulant tester Valgrind, je suis tombé sur un os : celui-ci me détecte une fuite mémoire, mais je ne sais pas, concrètement, d'où est-ce qu'elle peut venir.
Par conséquent, et pour comprendre encore davantage le fonctionnement de la mémoire, j'aurais aimé un petit coup de pouce pour y voir plus clair.

Merci d'avance pour les futures réponses.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
76
77
78
#include <stdio.h>
#include <stdlib.h>
 
#define Vide NULL
 
typedef struct CELLULE {
  int val;
  struct CELLULE *suiv;
} cellule;
 
typedef cellule *liste;
 
int estVide(liste l) {
  return l == Vide;
}
 
liste cons(int x, liste l) {
  cellule *elem;
  if ((elem = malloc(sizeof(cellule))) != NULL) {
    elem->val = x;
    elem->suiv = l;
    return elem;
  }
  return NULL;
}
 
liste inscr(int x, liste l) {
  if (estVide(l)) {
    return cons(x, Vide);
  } else {
    if (l->val > x) {
      return cons(x, l);
    } else {
      return cons(l->val, inscr(x, l->suiv));
    }
  }
}
 
liste tri_insertion(liste l) {
  if (estVide(l)) {
    return Vide;
  } else {
    return inscr(l->val, tri_insertion(l->suiv));
  }
}
 
void affiche(liste l) {
  if (estVide(l)) {
    printf("Liste vide");
  } else {
    for (cellule *e = l; e != NULL; e = e->suiv) {
      printf("%d ", e->val);
    }
  }
  printf("\n");
}
 
void destroy(liste *l) {
  cellule *tmp, *elem = *l;
  while (elem != NULL) {
    tmp = elem;
    elem = elem->suiv;
    free(tmp);
  }
  *l = NULL;
}
 
int main(void) {
  liste l = NULL;
  for (int i = 1; i <= 2; i++) {
    l = cons(i, l);
  }
  liste l2 = tri_insertion(l);
  affiche(l2);
  destroy(&l2);
  destroy(&l);
  return EXIT_SUCCESS;
}
Rapport Valgrind :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
==20791== Memcheck, a memory error detector
==20791== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20791== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20791== Command: ./ldsc
==20791== 
1 2 
==20791== 
==20791== HEAP SUMMARY:
==20791==     in use at exit: 8 bytes in 1 blocks
==20791==   total heap usage: 5 allocs, 4 frees, 40 bytes allocated
==20791== 
==20791== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==20791==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==20791==    by 0x8048474: cons (ldsc.c:19)
==20791==    by 0x80484C2: inscr (ldsc.c:29)
==20791==    by 0x8048545: tri_insertion (ldsc.c:43)
==20791==    by 0x8048534: tri_insertion (ldsc.c:43)
==20791==    by 0x8048624: main (ldsc.c:73)
==20791== 
==20791== LEAK SUMMARY:
==20791==    definitely lost: 8 bytes in 1 blocks
==20791==    indirectly lost: 0 bytes in 0 blocks
==20791==      possibly lost: 0 bytes in 0 blocks
==20791==    still reachable: 0 bytes in 0 blocks
==20791==         suppressed: 0 bytes in 0 blocks
==20791== 
==20791== For counts of detected and suppressed errors, rerun with: -v
==20791== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)
Comme vous pouvez le voir, c'est un programme test, la fuite se passe au niveau des appels récursifs, mais je ne vois pas ces fameuses pertes.
Bien sûr, plus la liste compte d'éléments, et plus la fuite est importante.