Tableau de tableau sur char ... problème free !
Bonjour,
Je veut éclater une chaîne de caracteres, pour cela j'alloue dynamiquement un: char ** pp_tab:
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 34 35
|
i = 0;
while ((tmp_word = strtok (tmp_str2, " ")) != NULL)
{
/* Allocation/Reallocation du tableau */
p_tmp = realloc (pp_tab, (i + 1) * sizeof (*pp_tab));
if (p_tmp == NULL)
{
err = 1;
break;
}
/* Mise a jour du pointeur. */
pp_tab = p_tmp;
/* Affectation du mot au tableau. */
pp_tab[i] = malloc ((sizeof (**pp_tab) * strlen (tmp_word)) + 1);
if (pp_tab[i] == NULL)
{
err = 1;
break;
}
strcpy (pp_tab[i], tmp_word);
pp_tab[i][strlen (tmp_word)] = '\0';
tmp_str2 = NULL;
i++;
}
pp_tab[i] = NULL; |
Pour la libération, je procède de cette manière:
Code:
1 2 3 4 5 6 7 8 9
|
i = 0;
printf ("Suppression du tableau:\n");
while (tab[i] != NULL)
{
free (tab[i]);
i++;
}
free (tab); |
Mais voilà le problème lors de l'exécution:
Citation:
*** glibc detected *** free(): invalid pointer: 0x0804c258 ***
Abandon
Où me suis-je trompé ?
Merci d'avance :P
PS: Dans mon test, j'ai 11 chaine d'allouée dans le tableau et j'ai pu constater que le problème viens lors de la liberation de la 10°, donc 2 avant celle qui vaut NULL..Cependant, je n'ai aucun problème lorsque parcours le tableau pour visionner le contenu !
Re: Tableau de tableau sur char ... problème free !
Citation:
Envoyé par CSoldier
Je veut éclater une chaîne de caracteres, pour cela j'alloue dynamiquement un: char ** pp_tab:
Mais voilà le problème lors de l'exécution:
Citation:
*** glibc detected *** free(): invalid pointer: 0x0804c258 ***
Abandon
PS: Dans mon test, j'ai 11 chaine d'allouée dans le tableau et j'ai pu constater que le problème viens lors de la liberation de la 10°, donc 2 avant celle qui vaut NULL..Cependant, je n'ai aucun problème lorsque parcours le tableau pour visionner le contenu !
Curieux, je n'ai pas de problèmes avec ce code (le tien est incomplet):
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static char ** crea(char *const str2)
{
/* ----- Decoupage de la chaine ----- */
/* Recherche des mots. */
char **pp_tab = NULL;
size_t i = 0;
char *tmp_str2 = str2;
char *tmp_word;
int err=0;
while ((tmp_word = strtok (tmp_str2, " ")) != NULL)
{
/* Allocation/Reallocation du tableau */
char **p_tmp = realloc (pp_tab, (i + 1) * sizeof (*pp_tab));
if (p_tmp == NULL)
{
err = 1;
break;
}
else
{
/* Mise a jour du pointeur. */
pp_tab = p_tmp;
/* Affectation du mot au tableau. */
pp_tab[i] = malloc (sizeof (**pp_tab) * strlen (tmp_word));
if (pp_tab[i] == NULL)
{
err = 1;
break;
}
strcpy (pp_tab[i], tmp_word);
}
tmp_str2 = NULL;
i++;
}
/* sentinel */
if (pp_tab != NULL)
{
pp_tab[i] = NULL;
}
return pp_tab;
}
static void disp(char **tab)
{
size_t i = 0;
while (tab[i] != NULL)
{
printf ("'%s'\n", tab[i]);
i++;
}
}
static void supp(char **tab)
{
size_t i = 0;
printf ("Suppression du tableau:\n");
while (tab[i] != NULL)
{
free (tab[i]);
i++;
}
free (tab);
}
int main(void)
{
char s[]="a b c d";
char ** tab = crea(s);
if (tab != NULL)
{
disp(tab);
supp(tab);
}
return 0;
} |