Problème de mémoire avec une liste chaînée
Bonjour à tous, j'ai un petit problème d'allocation mémoire mais je me demande vraiment d'où ça peut venir.
Voici mes structures
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
|
typedef struct nodeId
{
int id;
struct nodeId *next;
} nodeId;
typedef struct nodetag
{
char *word;
struct nodetag *next;
} nodetag;
typedef struct node
{
char *word;
int depth, type;
struct nodeId *nextId;
struct nodetag *nexttag;
struct node *child;
struct node *next;
} node;
typedef nodeId *Idlist;
typedef nodetag *taglist;
typedef node *tree; |
J'ai créé un arbre b de manière manuel pour tester mes fonctions.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
tree b = malloc(sizeof(tree));
b->nextId = malloc(sizeof(nodeId));
b->nextId->id = 3;
b->nextId->next = malloc(sizeof(nodeId));
b->nextId->next->id = 1;
b->nextId->next->next = malloc(sizeof(nodeId));
b->nextId->next->next->id = 8;
b->nextId->next->next->next = malloc(sizeof(nodeId));
b->nextId->next->next->next->id = 2;
b->nextId->next->next->next->next = NULL;
//J'ai effacer les autres initialisations pour que ce soit plus lisible
//Avant d'envoyer mon arbre dans les fonction je teste si le 8 s'affiche bien
printf("%d\n\n",b->nextId->next->next->id); //Il s'affiche correctement
int size_new_tree = counterLSC(b->nextId); //Calcul la longueur de l'id
int *new_tree = convertLSCtoTab(b->nextId, size_new_tree); //Mettre une LSC dans un tableau 1D |
La fonction qui ne va pas
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
|
int* convertLSCtoTab(Idlist a, int size)
{
printf("%d\n\n",a->next->next->id);//Affiche un nombre random
int i;
int *tab = NULL;
tab = malloc(size*sizeof(int)); //J'ai même essayé en allouant un tableau beaucoup plus grand qu'il n'en faut mais ça ne fonctionne pas
if (tab == NULL)
{
exit(0);
}
Idlist b = a;
for (i=0; a != NULL; i++)
{
tab[i] = a->id;
a = a->next;
}
int j;
for(j=0;j<i;j++)
{
printf("%d ", tab[j]); //Sa affiche 3 1 "un nbr random" 2
//Le 8 ne c'est pas affiché
}
a = b;
return tab;
} |
Merci pour votre aide.
Cordialement,
Airox