Erreur dans la fonction d'impression

cs_dark_cross Messages postés 7 Date d'inscription samedi 7 janvier 2006 Statut Membre Dernière intervention 3 décembre 2006 - 21 janv. 2006 à 15:06
gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010 - 21 janv. 2006 à 16:59
voici j'ai une erreur dans ma fonction d'impression si quelqu'un peux m'expliquer, je serais tres ravie.

#include <stdio.h>
#include <stdlib.h>

//speciale dedicasse Etienne Sigwald

//Structures de données

//creation de la case
typedef struct cell_
{
int elt;
struct cell_ *next;
}cell;

//definition du type

typedef cell* list;

//affichage de la liste

void print(list l)
{
printf("list : \n");
while(l!=NULL)
{
printf("%d\n",l->elt);
l = l->next;
}
printf("\n");
}

//initialisation de la liste

//insertion d'une cellule en debut de liste
list insertList(list l,int elt)

{
cell *c;
c=(cell*)malloc(sizeof(cell));
if (c==NULL)return NULL;
c->elt=elt;
c->next=l;
return c;//return le nouveau premier element

}
//insertion d'une cellule en fin de liste
list insertfinlist(list l, int elt)
{
list tmp=NULL, first=l;
cell *f;
f=(cell*)malloc(sizeof(cell));
// printf("bonjour1\n");
if(f==NULL)return NULL;
// printf("bonjouri2\n");
f->elt=elt;
f->next=NULL;
// printf("bonjour6\n");
while(l!=NULL)
{tmp=l;
l=l->next;
// printf("bonjouri3\n");
}
if(tmp!=NULL)
tmp->next=f;
return first;
}

void delete(list l)//supression de la liste
{
list t;//valeur temporaire
while(l!=NULL)
{
t=l;//mise en memoire de la premiere valeur
l=l->next;//passage a la valeur d'apres
free(t);//supression de la permiere valeur
}
}
//creation d'une liste circulaire

void circ(list l)
{
list t=NULL;
list first=l;
while(l!=NULL)
{
t=l;
l=l->next;
}
if(t!=NULL)
t->next=first;
}

int main()

{
cell *l=NULL;//initialisation de lit l (list l<-> cell *l)
l=insertList(l,12);//l devient le nouveau premier
l=insertfinlist(l,14);
print(l);//affichage de la liste !!!!!!!!!!!
delete(l);
// circ(l);
printf("ca marche\n");
print(l);
return(0);
}
voici les erreurs valgrind que j'ai: et je ne vois pas d'ou ca peux venir
list :
12
14

lolca marche
list :
20438==
20438== Invalid read of size 4
20438== at 0x8048433: print (liste-1.c:27)
20438== by 0x8048614: main (liste-1.c:116)
20438== Address 0x1BA43028 is 0 bytes inside a block of size 8 free'd
20438== at 0x1B90144F: free (vg_replace_malloc.c:235)
20438== by 0x804854E: delete (liste-1.c:88)
20438== by 0x80485F6: main (liste-1.c:113)
12
20438==
20438== Invalid read of size 4
20438== at 0x8048449: print (liste-1.c:28)
20438== by 0x8048614: main (liste-1.c:116)
20438== Address 0x1BA4302C is 4 bytes inside a block of size 8 free'd
20438== at 0x1B90144F: free (vg_replace_malloc.c:235)
20438== by 0x804854E: delete (liste-1.c:88)
20438== by 0x80485F6: main (liste-1.c:113)
14

lol==20438==

1 réponse

gagah1 Messages postés 509 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 3 août 2010
21 janv. 2006 à 16:59
Fait gaffe au pointeur.
Dans ta fn main(), tu supprimes list l avec delete(l) après tu l'affiche en appelant print(l).
0
Rejoignez-nous