IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

langage C et la structure pile


Sujet :

C

  1. #1
    Membre à l'essai
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Points : 19
    Points
    19
    Par défaut langage C et la structure pile
    salut tout le monde,
    mon but c'est de séparer les champs d'une ligne et les stocker dans une pile, ces champs dans la ligne (sous forme de chaine de caractères) sont séparés par des ","
    par exemple la ligne est:
    125895,A,8807,USER,TRD,1,TLX,
    pour cela j'ai fait cette fonction:

    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
    static pile* Sep_Line (char * line_in)
    {
     
    printf("Beginning of Sep_Line() for the line: %s \n", line_in);
     
       char* line_aux = malloc(sizeof(char) * MAX_LINE_LEN);
       printf("allocation for the line \n");
       char *field 	;
       field = malloc(sizeof(char) * MAX_FIELD_NAME_LEN);
     
       printf("allocation for pile \n");
       pile *ligne = malloc(sizeof(pile));
       printf("the allocation of pile is done \n");
     
       if (ligne == NULL)
       {
        printf("allocation failed \n ");
       }
     
       int i=0;
       int l;
       size_t len;
     
       strcpy(line_aux, line_in);
       printf("the line on which we are separating fields is : %s \n",line_aux); 
       strcat(line_aux, ",");
       printf("the line is now : %s \n", line_aux);
       strcat(line_aux,"\0");
       l= strlen(line_aux);
     
    /* Storing the line's content in a "pile" */
     
    do
    {
        len = strcspn(line_aux, ","); /* len contain the lenght of the initial segment of line_aux which doed not contain "," */
        printf("the length of the fields is :%d \n",len);
        strncpy (field, line_aux, len); /* field will contain the 1st field in the 1st iteration the second one in the second iteration ainsi de suite */
        strcat (field,"\0"); 
        printf("the field's value is : %s \n", field);
     
        Push(&ligne, field); /* In the first iteartion, the "pile" will contain the first field */
        printf("we stored the field's value in the pile \n");
        line_aux = line_aux + len + 2; /* To make line_aux points to the next field: line_aux will point to the caracter just following the "," */
     
    } 
     
    while (line_aux == "\0");
     
    printf("End of Sep_Line() \n \n");
     
    return (ligne);
     
    }
    la structure pile que j'utilise est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    typedef struct pile {
       char* content;
       struct pile * prev;
      } pile;
    et la fonction Push est:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void Push(pile **p, char *Val)
     
    {
            pile *element= malloc(sizeof(pile));
            if (element == NULL)  /* If the allocation is failed. */
                    {
                    exit(EXIT_FAILURE);
                    }
            element->content = Val;
            element->prev = *p;
            *p = element;       /* The pointer points to the last element */
    }
    lorsque j'exècute la fonction Sep_Line , il s'arrête au message:
    the field's value is : BRDG_TAG
    et il m'affiche : Segmentation Fault
    je comprends pas où est la Segmentation Fault!!??
    SVP quelqu'un pourrait m'aider !! c'est urgent
    et merci

  2. #2
    Membre régulier
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2011
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2011
    Messages : 59
    Points : 120
    Points
    120
    Par défaut
    Salut,

    à première vue, les entrées dans la pile vont collisionner, tu prends tjrs le même pointeur qui sera écrasé par la suite, je pense qu'il faut un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     element->content = strdup(Val)
    dans la fonction Push

    ensuite j'ai un doute sur le while(line_aux == "\0") à moins que tes données d'entrée aient un formatage spécial

    Cdt

  3. #3
    Membre à l'essai
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Points : 19
    Points
    19
    Par défaut
    bonjour ,
    même avec le strdup j'ai toujours la segmentation fault

  4. #4
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Beaucoup d'erreurs :
    Push() : déjà signalée
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
            element->content = strdup(Val); //     element->content = Val;
    Sep_Line() :
    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
    ....
    // printf("allocation for pile \n");
       pile *ligne = NULL;    // malloc(sizeof(pile));
    // printf("the allocation of pile is done \n");
    // if (ligne == NULL)
    // {
    //    printf("allocation failed \n ");
    // }*/
    //  int i=0; 
    //  int l;
    .....
         printf("the line is now : %s \n", line_aux);
     //  strcat(line_aux,"\0"); inutile déjà mis par le strcat() précédent
     //  l= strlen(line_aux);   inutile
    .....
       strncpy (field, line_aux, len); /* field will contain the 1st field in the 1st iteration the second one in the second iteration ainsi de suite */
       field[len] = '\0'; // strcat (field,"\0");  faux : strncpy ne met pas (toujours) un 0 terminal :
                          // strcat inutilisable
    .....
      line_aux = line_aux + len + 1; // pas +2
    .....
    while (line_aux[0] != '\0'); //while (line_aux == "\0");
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  5. #5
    Membre à l'essai
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Points : 19
    Points
    19
    Par défaut
    J'ai fais les modifications dont vous m'avez signalez
    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
    static pile* Sep_Line (char * line_in)
    {
     
    printf("Beginning of Sep_Line() for the line: %s \n", line_in);
    trace("Beginning of Sep_Line() \n");
     
       char* line_aux = malloc(sizeof(char) * MAX_LINE_LEN);
       char *field 	;
       field = malloc(sizeof(char) * MAX_FIELD_NAME_LEN);
       pile *ligne = NULL;
     
       int i=0;
       int L;
       size_t len;
     
       strcpy(line_aux, line_in);
       strcat(line_aux, ",");
       strcat(line_aux,"\0");
       printf("the line is now : %s \n", line_aux);
     
    /* Storing the line's content in a "pile" */
     
    do
    {
        len = strcspn(line_aux, ","); /* len contains the lenght of the initial segment of line_aux which doed not contain "," */
        printf("the field's length is :%d \n", len);
        strncpy (field, line_aux, len); /* field will contain the 1st field in the 1st iteration the second one in the second iteration ainsi de suite */
        field[len]='\0';
        printf("the field's value is : %s \n", field);
     
        Push(&ligne, field); /* In the first iteartion, the "pile" will contain the first field */
        printf("we stored the field's value in the pile \n");
        line_aux = line_aux + len + 1; /* To make line_aux points to the next field: line_aux will point to the caracter just following the "," */
        printf("the rest from the line is :%s \n",line_aux);
        View(ligne);
    } // fin de do
    while (line_aux[0] != '\0'); 
    printf("End of Sep_Line() \n \n");
    return (ligne);
    }
    et l'exécution me donne cela:

    Beginning of Sep_Line() for the line: BRDG_TAG,ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE
    the line is now : BRDG_TAG,ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE ,
    the field's length is :8
    the field's value is : BRDG_TAG
    we stored the field's value in the pile
    the rest from the line is :ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE ,
    BRDG_TAG
    the field's length is :10
    the field's value is : ACTION_IND
    Segmentation Fault


    always a segmentation fault
    whhhhhhhyyyyyyy!!!!!

  6. #6
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Septembre 2007
    Messages
    7 372
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 372
    Points : 23 628
    Points
    23 628
    Par défaut
    Citation Envoyé par ensi_meriem Voir le message
    always a segmentation fault
    whhhhhhhyyyyyyy!!!!!
    Est-ce que tu sais ce qu'est une segmentation fault, déjà ? Si non, ça te ferait gagner beaucoup de temps de commencer par cela …

  7. #7
    Membre à l'essai
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Points : 19
    Points
    19
    Par défaut
    une segmentation faut c lorsqu'un programme essaye d'accéder à une zone mémoire qui ne lui ai pas allouée
    c'est ça n'est ce pas!!

  8. #8
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    1- Comme déjà signalé, ces lignes ne servent à rien :
    2- Le code que tu montres donne chez moi (après mise en commentaires de trace() et View()) un résultat qui semble correct sans erreurs de segmentation.

    Beginning of Sep_Line() for the line: BRDG_TAG,ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE
    the line is now : BRDG_TAG,ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE,
    the field's length is :8
    the field's value is : BRDG_TAG
    we stored the field's value in the pile
    the rest from the line is :ACTION_IND,BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE,
    the field's length is :10
    the field's value is : ACTION_IND
    we stored the field's value in the pile
    the rest from the line is :BORG_CODE,BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE,
    the field's length is :9
    the field's value is : BORG_CODE
    we stored the field's value in the pile
    the rest from the line is :BORG_XCOD_CODE,BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE,
    the field's length is :14
    the field's value is : BORG_XCOD_CODE
    we stored the field's value in the pile
    the rest from the line is :BFUN_CODE,ADDR_NUM,CONTACT_TYPE,CONTACT_CODE,
    the field's length is :9
    the field's value is : BFUN_CODE
    we stored the field's value in the pile
    the rest from the line is :ADDR_NUM,CONTACT_TYPE,CONTACT_CODE,
    the field's length is :8
    the field's value is : ADDR_NUM
    we stored the field's value in the pile
    the rest from the line is :CONTACT_TYPE,CONTACT_CODE,
    the field's length is :12
    the field's value is : CONTACT_TYPE
    we stored the field's value in the pile
    the rest from the line is :CONTACT_CODE,
    the field's length is :12
    the field's value is : CONTACT_CODE
    we stored the field's value in the pile
    the rest from the line is :
    End of Sep_Line()
    As-tu corrigé le Push() ?
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  9. #9
    Membre à l'essai
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Points : 19
    Points
    19
    Par défaut
    bizzaaaarre
    oui j'ai corrigé le Push depuis toute à l'heure:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    void Push(pile **p, char *Val)
     
    {
            pile *element= malloc(sizeof(pile));
            if (element == NULL)  /* If the allocation is failed. */
                    {
                    exit(EXIT_FAILURE);
                    }
            element->content = strdup(Val);
            element->prev = *p;
            *p = element;       /* The pointer points to the last element */
    }
    la fonction View sert à imprimer le contenu de la pile

    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    void View(pile *p)
    {
            while(p)
              {
                 printf("%s\n",p->content);
                 p = p->prev;
              }
    }

  10. #10
    Membre à l'essai
    Profil pro
    Étudiant
    Inscrit en
    Février 2010
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2010
    Messages : 32
    Points : 19
    Points
    19
    Par défaut
    bonjour,
    je travaille toujours avec la même structure :

    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    typedef struct pile {
       char *content;
       struct pile * prev;
      } pile;

    avec la fonction pop suivante:
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    char* Pop(pile **p)
     
    {
            char *Val = (char*)malloc(sizeof(char) *MAX_FIELD_NAME_LEN);
            pile *tmp; 
    	if(p == NULL) return ("\n");     /* Returns \n if the "pile is empty */
            tmp = (*p)->prev;
            Val = (char*)(*p)->content;
            free(*p);
            *p = tmp;       /* The pointer points to the last element */
    	printf("the value of the Pop is: %s  \n",Val);
            return Val;     /* Returns the value popped from the "pile" */
    }

    lorsque je fais appel à cette fonction, on m'affiche la trace mais pas la valeur dépilée!!!
    c'est ce qu'on m'affiche:
    We are popping the field number 8 from the file borg_test1.csv
    Before the Pop()
    the value of the Pop is:
    the col name is
    the value of the Pop is:
    the value of the col is
    Beginning of create_query()
    End of create_query()

    la fonction pop est-elle erronée??
    Merci en avance

  11. #11
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    char* Pop(pile **p)
     
    {
    ....    
            Val = strdup((*p)->content);
            free(*p);
            *p = tmp;       /* The pointer points to the last element */
            printf("the value of the Pop is: %s  \n",Val);
            return Val;     /* Returns the value popped from the "pile" */
    }
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  12. #12
    Membre confirmé
    Inscrit en
    Juillet 2005
    Messages
    512
    Détails du profil
    Informations forums :
    Inscription : Juillet 2005
    Messages : 512
    Points : 641
    Points
    641
    Par défaut
    Tu alloue un bloc pour Val !
    Puis tu lui affecte une autre adresse plus loin !
    Ton bloc est donc perdu à jamais

    Si tu voulais copier la chaine pointé par content dans val, il y a des fonction pour cela ! Mais surtout pas l'opérateur d'affectation dans ce cas.

    Je ne sais pas ou tu as prévu de désallouer le bloc pointé par Val crée avec malloc ? mais il va falloir y penser.

Discussions similaires

  1. pile langage c
    Par baylamat dans le forum C
    Réponses: 3
    Dernier message: 27/12/2006, 15h28
  2. Structure du langage et fonctions
    Par $@&_# dans le forum Langage
    Réponses: 1
    Dernier message: 26/11/2006, 20h11
  3. [langage] Titiller la pile ?
    Par kij dans le forum Langage
    Réponses: 7
    Dernier message: 26/04/2005, 19h12
  4. [langage] [traduction] Structure SWITCH
    Par rbh dans le forum Langage
    Réponses: 6
    Dernier message: 07/06/2004, 17h24
  5. [langage] structures complexes et affichage
    Par mat21 dans le forum Langage
    Réponses: 5
    Dernier message: 18/02/2004, 15h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo