probleme de fuite de memeoire

a marqué ce sujet comme résolu.

Bonjour,

Je doit rendre un projet en C dont le but est de manipuler des graphes.J'ai un problème de fuite de mémoire dans min programme et je cherche quelqu'un pour m'expliquer les erreurs dans mon code avec valgrind cordialement

 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
==6463== Memcheck, a memory error detector
==6463== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6463== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==6463== Command: ./a.out
==6463== 
il y a 10 ars dans le fichier
il y a 8 sommets dans le fichier sommet ds la fonction 8
2--1
1--5
5--2
3--5
7--7
8--1
3--1
2
1
5
3
7
8
3
==6463== Invalid write of size 8
==6463==    at 0x4C2FD73: __GI_memcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6463==    by 0x4EB0412: _IO_file_xsgetn (fileops.c:1387)
==6463==    by 0x4EA593E: fread (iofread.c:42)
==6463==    by 0x400C22: symetrisation (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x400F00: lirePfg (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x401120: main (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==  Address 0x51fcd00 is 32 bytes inside a block of size 36 alloc'd
==6463==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6463==    by 0x400C01: symetrisation (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x400F00: lirePfg (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x401120: main (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463== 
==6463== Invalid write of size 8
==6463==    at 0x4C2FD73: __GI_memcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6463==    by 0x4EB0412: _IO_file_xsgetn (fileops.c:1387)
==6463==    by 0x4EA593E: fread (iofread.c:42)
==6463==    by 0x400C5F: symetrisation (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x400F00: lirePfg (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x401120: main (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==  Address 0x51fcd20 is 16 bytes after a block of size 48 in arena "client"
==6463== 

valgrind: m_mallocfree.c:304 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 112, hi = 12884901889.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata.  If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away.  Please try that before reporting this as a bug.


host stacktrace:
==6463==    at 0x3805DB16: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x3805DC24: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x3805DDA6: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x3806AB83: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x380571EB: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x38055CCB: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x38059B3B: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x380552C7: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x38000AD9: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==6463==    by 0x802C460D1: ???
==6463==    by 0x802B99EEF: ???

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable
==6463==    at 0x400C7E: symetrisation (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x400F00: lirePfg (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)
==6463==    by 0x401120: main (in /home/fabien/Bureau/projet_C_13mai/tpc-soutien2/pfg_vers_sommet/a.out)

Salut,

Sans le code, ça va être difficile d'être plus explicite que Valgrind lui-même… Il te dit ligne 47 que ton programme écrit vraisemblablement en dehors de la mémoire qui lui a été allouée, et que ça se passe dans la fonction symetrisation. Ça peut être aussi simple qu'un accès en dehors de la limite d'un tableau, comme ça peut être un truc un peu plus délicat à trouver. Mais encore une fois, sans le code, on ne peut pas faire de miracle.

+0 -0

Merci beaucoup je vous envoie le code

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# include <stdlib.h>
# include <stdio.h>
# include <math.h>
# include <string.h>


# define NOM_FICHIER1 "Exemple.pfg"
# define NOM_FICHIER2 "CondMat.pfg"
# define NOM_FICHIER3 "web-BerkStan.pfg"

typedef struct listadj{
  struct sommet *som;
  struct listadj *suiv;

}listadj_t;


typedef struct sommet{
  int numero;
  struct listadj *tete;
  struct sommet *suiv;
  int marque; //pour le parcours
}som_t;


som_t *tete;//initialisation de la tete de lecture de la liste de sommets

typedef struct graphe{

  int m; // nbre somm   
  int n; // nbre arc
  struct sommet *s;

}grap_t;


enum{   
  NO_ERROR=0,
  ERR_OUVERTURE=1,
  ERR_FERMETURE=2   
};


//Determination nb arcs

int nb_arcs(FILE *pfg)
{   
  int nb_arcs=0;
  int taille=0;
  fseek(pfg,0,SEEK_END);
  taille=ftell(pfg);
  rewind(pfg);
  nb_arcs=taille/8;
  return nb_arcs;   
}


//Determination nb sommetss

int nb_sommets(FILE *pfg)
{   
  int max=0;//*nombre de sommets
  int tmp=0;
  fseek(pfg,0,SEEK_SET);
  do{
    fread(&tmp,sizeof(int),1,pfg);
    if(max<tmp){
      max=tmp;
    }
  }while(!feof(pfg));
  return max;   
}




grap_t *initialisation_graph(int m, int arc){


  listadj_t *liste=malloc(sizeof(listadj_t));

  liste->som=NULL;
  liste->suiv=NULL;

   som_t *debut=malloc(sizeof(som_t));

  debut->numero=0;
  debut->tete=NULL;
  debut->marque=0;
  debut->suiv=NULL;
  debut=tete;

  grap_t *graph= malloc(sizeof(grap_t));
  graph->m=m;
  graph->n=arc;
  graph->s=NULL;

  return graph;
}



void insertion_en_fin( int tmp1){

  som_t *nouv=malloc(sizeof(som_t));
  som_t *ptr=malloc(sizeof(som_t));
  nouv->numero=tmp1;
  nouv->marque=0;
  ptr=tete;
  if(ptr==NULL)
    {
      nouv->suiv=tete;
      tete=nouv;
    }
  else
    {
      while(ptr->suiv!= NULL){ //attention  il faut tenir compte de l'avant dernier element est celui a modifier
        ptr=ptr->suiv;
      }
      nouv->suiv=NULL;
      ptr->suiv=nouv;
    }       
}

int compar(const void *a, const void *b) {
  const int *i1 = a , *j1 = a+sizeof(int), *i2 = b , *j2 = b+sizeof(int);
  if(*i1<*i2 || (*i1 == *i2 && *j1 < *j2 )) return -1;
  // premier arc inferieur lexicographiquement au second
  else if( *i1 == *i2 && *j1 == *j2) return 0;
  // arcs egaux
  else return 1;
  // sinon le premier est superieur au second
}

/*symetrisation*/
void symetrisation(int m, FILE *pfg){
  int i=0;
  int tmp=0;
  // allouer un tableau de 2m arc
  int *t=(int*)malloc(4*m+sizeof(int));
  // 1ere lecture
  fread(t,sizeof(int),2*m,pfg);
  // 2eme lecture
  rewind(pfg);

  fread(t+2*m,sizeof(int),2*m,pfg);

  //inversion des arcs de la partie droite

  for(i=2*m;i<(4*m);i++){
    tmp=t[i];
    t[i]=t[i+1];
    t[i+1]=tmp;
  }

  // tracer le tableau en ordre lexicographique
  qsort(t,2*m,2*sizeof(int),compar);
  // compar dans le TP3 à faire
  // Construire le graphe en evitant les doublons
  for(i=0;i<(4*m-1);i+=2){
    if(i>(4*m-3) || ((t[i]!=t[i+2]) && (t[i+1]!=t[i+3]))){
      //inserer_arc(t[i],t[i+1]);
      // inserer_arc à créer
    }
  }
  free(t);
}


int lecture(FILE *pfg){
  int tmp1,tmp2,ecriture;
  rewind(pfg);

  while(fread(&ecriture,sizeof(int),1,pfg)==1){
    fread(&tmp1,sizeof(int),1,pfg);
    fread(&tmp2,sizeof(int),1,pfg);
    printf("%d--%d\n",tmp1,tmp2);
    insertion_en_fin(tmp1);
  }

  return 0;

}



void affichage(){

  som_t * ptr=malloc(sizeof(som_t));
  ptr=tete;
  while(ptr!=NULL){
    printf("%d\n",ptr->numero);
    //printf("%d\n",ptr->marque);
    ptr=ptr->suiv;

  }
}

//Lecture du pfg et mise en memeoire de celui ci 

struct graphe *lirePfg(FILE *pfg,int m, int arc){


  //1 allouer un struct graphe *
  grap_t *graph=NULL;
  //listadj_t *liste=NULL;
  //som_t *debut=NULL;

  //2 ouvre fichier pfg
  // deja fait

  //3 compter les sommets
  //Determination nb arcs

  arc= nb_arcs(pfg);

  //Determination nb sommetss

  m=nb_sommets(pfg);
  printf(" sommet ds la fonction %d\n",m);

  graph= initialisation_graph(m,arc);
  //liste=initialisation_liste();
  //debut=initialisation_sommet();
  rewind(pfg);

  //relis chaque arc pour avoir valeur de chaque arc
  //4 allouer G-> S
  initialisation_graph( m,arc);

  //5 rembobiner pfg et lire arc par arc
  // lecture pfg permet la lecture du pfg et l'insertion des sommets dans la liste chainnes 
  lecture(pfg);
  // vérification
  affichage();
  //symetrisation
  rewind(pfg);
  symetrisation( m,pfg);
  // arc entre t[0] et t[1]
  //rajouter t[1] dans la liste d'adjacence de t[0] (en queue si possible   
  // charger graphe 
  return graph;
}

//Liberation de la memoire

void liberer_graphe( grap_t *graph){

    if(graph!=NULL){
        free(graph->s);
        free(graph);
        }

}

void liberer_insertion(som_t *nouv, som_t *ptr){

  free(nouv);
  free(ptr);
}

//Conversion de PFG en DOT

void conversion_pfg_dot(FILE * fichier_pfg, FILE *fichier_dot){
  int ecriture;
  int tmp1, tmp2;
  fprintf(fichier_dot,"digraph {");
  while(fread(&ecriture,sizeof(int),1,fichier_pfg)==1){
    fread(&tmp1,sizeof(int),1,fichier_pfg);
    fread(&tmp2,sizeof(int),1,fichier_pfg);
    fprintf(fichier_dot,"%d->%d\n",tmp1,tmp2);
  }
  fprintf(fichier_dot,"}");
}


//Dtermination des erreurs

void expliquer_erreur(int code_erreur) {

  switch (code_erreur) {
  case NO_ERROR:
    printf("Copie effectuée avec succès\n");
    break;
  case ERR_OUVERTURE:
    printf("Impossible d'ouvrir le fichier source en lecture\n");
    break;
  case ERR_FERMETURE:
    printf("Impossible de fermer le fichier source\n");
    break;
  default:
    printf("Erreur inconnue: %d\n", code_erreur);
    break;
  }
}



int main (int argc, char *argv[]){

  // Declaration des variables

  int n=0; // nb d'arcs
  int m=0; // nb de sommets
  int code_erreur=0;


  //Fichier PFG
  FILE *pfg=fopen(NOM_FICHIER1,"rb");

  // FICHIER DOT
  // Création d'un fichier DOT à partir d'un PFG
  FILE *dot=fopen("test.dot","wt");

  // vérification d'ouverture des fichiers
  if(pfg==NULL){
    return ERR_OUVERTURE;
  }
  if(dot==NULL){
    return ERR_OUVERTURE;
  }

  // Fonction qui calcule le nombres d'arcs
  n=nb_arcs(pfg);
  // Verification
  printf("il y a %d ars dans le fichier\n",n);

  // Fonction qui calcule le nombres de sommets
   m=nb_sommets(pfg);
//  Verification
  printf("il y a %d sommets dans le fichier",m);

  //appel des fonctions

  //printf("%d\n",lect);



  grap_t *graph=lirePfg(pfg,m,n);

  liberer_graphe(graph);


  // Fonction réussi 

  //conversion_pfg_dot(pfg, dot);

  //Vérification Fermeture du fichier


  if(fclose(pfg)!=0){
    return ERR_FERMETURE;
  }
  if(fclose(dot)!=0){
    return ERR_FERMETURE;

  }
  expliquer_erreur(code_erreur);

  return 0;
}
+0 -0
Connectez-vous pour pouvoir poster un message.
Connexion

Pas encore membre ?

Créez un compte en une minute pour profiter pleinement de toutes les fonctionnalités de Zeste de Savoir. Ici, tout est gratuit et sans publicité.
Créer un compte