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;
  }