Bonjour
J'ai programmé une pile FILO d'entiers en C.
Et je me suis dit que j'allais tester le code avec valgrind qui me sort deux erreurs que je ne comprends pas. Je suis novice avec cet outil alors je ne sais pas si se sont des faux positifs ou si je dois en tenir compte.
Voila le code:
Voila la ligne de commande avec laquelle je compile:
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 #include <stdio.h> #include <stdlib.h> /* * Element de la pile. */ struct Element { int value; // Le contenu de la liste chainée struct Element *next; // L'élément suivant de la liste chainée. }; typedef struct Element* Stack; Stack initialize(int value); void push(Stack* stack, int value); int main(void) { Stack stack = NULL; push(&stack, 0); free(stack); return 0; } /* * Crée, alloue dynamiquement et initialise un élément. * @param value La valeur de l'élément. * @return Si l'allocation réussit, retourne l'élément initialisé * dynamiquement, sinon retourne la valeur NULL. */ Stack initialize(int value) { Stack stack = NULL; stack = malloc(sizeof(stack)); if(stack != NULL) { stack->value = value; } else { perror("Impossible d'allouer dynamiquement un élément"); } return stack; } /* * Ajoute un élément dans la pile. * @param stack Un pointeur de pointeur sur la pile dans laquelle l'élément sera ajouté. * @param value La valeur à ajouter dans la pile. */ void push(Stack* stack, int value) { Stack element = initialize(value); element->next = (*stack); (*stack) = element; }
valgrind --tool=memcheck --leak-check=full --leak-resolution=high --show-reachable=yes ./integerStack
Et voila les erreurs:
Quelqu'un pourrait-il m'expliquer le problème
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 ==9766== Invalid write of size 4 ==9766== at 0x80484EC: push (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== by 0x804846E: main (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== Address 0x418502c is 0 bytes after a block of size 4 alloc'd ==9766== at 0x4022AB8: malloc (vg_replace_malloc.c:207) ==9766== by 0x80484AB: initialize (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== by 0x80484E0: push (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== by 0x804846E: main (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== ==9766== Invalid read of size 4 ==9766== at 0x80485C7: printStack (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== by 0x8048479: main (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== Address 0x418502c is 0 bytes after a block of size 4 alloc'd ==9766== at 0x4022AB8: malloc (vg_replace_malloc.c:207) ==9766== by 0x80484AB: initialize (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== by 0x80484E0: push (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack) ==9766== by 0x804846E: main (in /media/KINGSTON/Cours/CoursAlgo/CM/integerStack)
ET l'erreur
Partager