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 :

Erreur de realloc()


Sujet :

C

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2018
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 25
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2018
    Messages : 3
    Points : 6
    Points
    6
    Par défaut Erreur de realloc()
    Bonsoir,

    J'ai un gros problème au niveau de mon realloc(), a vrai dire, je n'arrive toujours pas a savoir pourquoi :

    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
    54
    55
     
    unsigned char** lecture_Dico() {
     
      FILE* fichier_in = NULL;
      fichier_in = fopen("liste.de.mots.francais.latin1.txt", modeOuverture_in);
     
      if (fichier_in != NULL) {
     
        unsigned char** tab_Fichier_in = malloc(1 * sizeof(unsigned char*));
        tab_Fichier_in[0] = malloc(5 * sizeof(unsigned char));
        tab_Fichier_in = realloc(tab_Fichier_in, 100 *sizeof(unsigned char*)); // FONCTIONNE
     
        unsigned char temp;
        long nb_mot = 0;
        int longeur_Mot = 0;
        unsigned char tab_temp[4];
     
        while (fscanf(fichier_in, "%c", &temp) != EOF) {
     
          if ( !(temp == 10) ) {
            if (longeur_Mot < 4) {
              tab_temp[longeur_Mot] = temp;
              printf("\n%c", temp);
              printf("longeur %d", longeur_Mot);
            }
            longeur_Mot++;
            printf("Longeur ++ %d", longeur_Mot);
          }
          else {
            if (longeur_Mot < 5) {
              printf("nb_mot %ld", nb_mot);
              for ( int i = 0; i < longeur_Mot; i++) {
                tab_Fichier_in[nb_mot][i] = tab_temp[i];
              }
              nb_mot++;
              tab_Fichier_in[longeur_Mot] = '\0';
              //tab_Fichier_in = realloc(tab_Fichier_in, nb_mot+2 *sizeof(unsigned char*)); // NE FONCTIONNE PAS
              tab_Fichier_in[nb_mot] = malloc(5 * sizeof(unsigned char));
            }
            //printf("\n");
            longeur_Mot = 0;
          }
        }
        printf("\n");
        for (long i = 0; i <= 20; ++i) {
          printf("Mot numero: %ld ---> %s\n", i, tab_Fichier_in[i]);
     
        }
        return tab_Fichier_in;
      }
     
      else {
        exit(-1);
      }
    }
    Suivant l'endroit ou je realloc, elle fonctionne (ligne 11) ou non (ligne 37) et a vrai dire je ne comprends pas pourquoi j'obtiens ça :*** Error in `./xorcipher': realloc(): invalid next size: 0x00000000016c9240 ***

    Ensuite, je lis un fichier contenant :
    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
     
    fax
    fax
    fax
    fax
    fax
    fax
    fax
    fax
    fax
    fax
    fax
    fax
     
    //APRES FONCTION
     
    Mot numero: 0 ---> fax
    Mot numero: 1 ---> fax
    Mot numero: 2 ---> fax
    Mot numero: 3 ---> (null)
    Mot numero: 4 ---> fax
    Mot numero: 5 ---> fax
    Mot numero: 6 ---> fax
    Mot numero: 7 ---> fax
    Mot numero: 8 ---> fax
    Mot numero: 9 ---> fax
    Mot numero: 10 ---> fax
    Mot numero: 11 ---> fax
    J'obtiens une valeur NULL sans non plus savoir pourquoi, étant donné dans les mots sont les mêmes.

    Merci d'avance pour votre aide, Florian.

    PS: Excusez-moi de ne pas être vraiment explicite la-dessus.

    EDIT:

    Voici le code fonctionnel !
    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
     
    unsigned char** lecture_Dico() {
     
      FILE* fichier_in = NULL;
      fichier_in = fopen("liste.de.mots.francais.latin1.txt", modeOuverture_in);
     
      if (fichier_in != NULL) {
     
        unsigned char** tab_Fichier_in = malloc(1 * sizeof(unsigned char*));
        tab_Fichier_in[0] = malloc(5 * sizeof(unsigned char));
        //tab_Fichier_in = realloc(tab_Fichier_in, 100 *sizeof(unsigned char*));
     
        unsigned char temp;
        long nb_mot = 0;
        int longeur_Mot = 0;
        unsigned char tab_temp[4];
        while (fscanf(fichier_in, "%c", &temp) != EOF) {
     
          if ( !(temp == 10) ) {
            if (longeur_Mot < 4) {
              tab_temp[longeur_Mot] = temp;
              printf("\n%c", temp);
              printf("longeur %d", longeur_Mot);
            }
            longeur_Mot++;
            printf("Longeur ++ %d", longeur_Mot);
          }
          else {
            if (longeur_Mot < 5) {
              printf("nb_mot %ld", nb_mot);
     
              tab_Fichier_in = realloc(tab_Fichier_in, (nb_mot+2) *sizeof(unsigned char*));
              tab_Fichier_in[nb_mot+1] = malloc(5 * sizeof(unsigned char));
              for ( int i = 0; i < longeur_Mot; i++) {
                tab_Fichier_in[nb_mot][i] = tab_temp[i];
              }
              tab_Fichier_in[nb_mot][longeur_Mot] = '\0';
              nb_mot++;
            }
            //printf("\n");
            longeur_Mot = 0;
          }
        }
        printf("\n");
        for (long i = 0; i <= 20; ++i) {
          printf("Mot numero: %ld ---> %s\n", i, tab_Fichier_in[i]);
     
        }
        return tab_Fichier_in;
      }

  2. #2
    Expert confirmé
    Inscrit en
    Mars 2005
    Messages
    1 431
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 1 431
    Points : 4 182
    Points
    4 182
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tab_Fichier_in = realloc(tab_Fichier_in, (nb_mot+2) *sizeof(unsigned char*));
    Quelles seront les conséquences d'un échec de realloc ici ?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. realloc erreur avec valgrind
    Par lilington dans le forum Débuter
    Réponses: 6
    Dernier message: 12/05/2011, 03h46
  2. erreur segmentation realloc
    Par Msakeni dans le forum Débuter
    Réponses: 4
    Dernier message: 28/02/2010, 08h23
  3. erreur de realloc
    Par Mr_Chut dans le forum C
    Réponses: 5
    Dernier message: 09/07/2006, 01h42
  4. erreur IDL:omg.org/CORBA/MARSHAL:1.0
    Par Pinggui dans le forum CORBA
    Réponses: 3
    Dernier message: 13/05/2002, 15h05
  5. [Kylix] Erreur objet
    Par Anonymous dans le forum EDI
    Réponses: 1
    Dernier message: 22/03/2002, 09h41

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