Problème de mémoire avec une liste chaînée

Résolu/Fermé
Airox Messages postés 146 Date d'inscription vendredi 1 août 2014 Statut Membre Dernière intervention 5 novembre 2018 - Modifié le 9 nov. 2017 à 01:08
Airox Messages postés 146 Date d'inscription vendredi 1 août 2014 Statut Membre Dernière intervention 5 novembre 2018 - 9 nov. 2017 à 10:12
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

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.

    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
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
A voir également:

1 réponse

Airox Messages postés 146 Date d'inscription vendredi 1 août 2014 Statut Membre Dernière intervention 5 novembre 2018 13
9 nov. 2017 à 10:12
Problème d'initialisation de l'arbre.
0