Rebonjour tout le monde.

J'aurais une question par rapport a l'effacement de char*

En fait, je suis en train de coder un programme qui me lit un fichier, et me remplit des variables par rapport a ce fichier. J'utilise plusieurs fois les memes *char .

Donc je free a chaque fois + malloc les meme variables pour les reutiliser.
Sur le terminal>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
Commentaires
Salles: 10
Monstre: position 30 /*devrait etre 3, il garde le 0 en memoire on dirait*/
Monstre: lvl 42 /* OK */
Monstre: position 942 /* devrait etre 9, la il garde le 41, les 2 derniers chiffres*/
Monstre: lvl 22 /* devrait etre 2, recup le 2 d'avant */
Donc, ca se decalle d'une etrange facon! Et je ne sais pas pk. J'ai ptetre mal compris le systeme de malloc & free.

Mon code, si ca peut aider :/
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
int read_file(char *file)
{
  int rd,op,i,j,rooms,position,lvl;
  char *buf,*tmp,*tmp2;
  buf = malloc(sizeof(char));
  s_ListeChainee liste_salles;
 
 
 
 /* Ouverture du fichier */
 
 op = open (file,O_RDONLY );
 check_open(op); /*controle ouverture*/
 
 while(read(op,buf,1)>0)
   {
     if ( buf[0] == '#')
       {
	 while(buf[0] != '\n')
	   {
	     read(op,buf,1); 
	   }
	 printf("Commentaires\n");
       }
     else if ( buf[0] == 'R')
       {
	 tmp = malloc(sizeof(char));
	 tmp2 = malloc(sizeof(char));
	 i=0;
	 while(buf[0] != '\n')
	   {
	     tmp[i++] = buf[0];
	     read(op,buf,1); 
	   }
	 i=0;
	 while (tmp[i] != ' ')
	   {
	     i++;
	   }
	 i++;
	 j=0;
	 while (tmp[i] != '\0')
	   {
	     tmp2[j] = tmp[i]; 	 
	     i++;
	     j++;
	   }
	 rooms = atoi(tmp2);
	 printf("Salles: %i\n",rooms);
	 free(tmp);
	 free(tmp2);
       } /**** Monstres***/
     else if ( buf[0] == 'M')
       { 
	 tmp = malloc(sizeof(char));
	 tmp2 = malloc(sizeof(char));
 
	 i=0;
	 while(buf[0] != '\n')
	   { 
	     tmp[i++] = buf[0];
	     read(op,buf,1); 
	   }
	 printf("Strlen %d\n",strlen(tmp));
	 i=0;
	 while (tmp[i] !=' ')
	   { //printf("tmp[%i] = %c \n",i,tmp[i]);
	     i++;
	   }
	 printf("position de i: %i\n",i);
	 free(tmp2);
	 tmp2 = malloc(sizeof(char));
	 j =0; 
	 i++;
	 while (tmp[i] !=' ')
	   { 
	     //printf("tmp[%i] = %c \n",i,tmp[i]);
	     //printf("\npidou2\n");
	     tmp2[j] = tmp[i];
	     //printf("tmp nb 1: %c \n",tmp[i]);
	     //printf("tmp2 nb 1: %c \n",tmp2[j]);
	     i++;
	     j++;
	   }
	 position = atoi(tmp2);
	 printf("Monstre: position %i\n",position);
 
	 free (tmp2);
	 /*** Nouvelle partie***/ 
	 tmp2 = malloc(sizeof(char));	 
	 j =0; 
	 while (tmp[i] != '\0')
	   {
	     //printf("\npidou3\n");
	     //printf("tmp nb 2: %c \n",tmp[i]);
	     tmp2[j] = tmp[i]; 	 
	     //printf("tmp2 nb 2: %c \n",tmp2[j]);
	     i++;
	     j++;
	   } 
	 lvl = atoi(tmp2);
	 printf("Monstre: lvl %i\n",lvl);
	 free(tmp);
	 free(tmp2);
            }
Merci d'avance! Je n'y comprend rien