"pointer being freed was not allocated"
Bonjour a tous,
Je suis entrain d'essayer de free un **tab mais j'obtiens une erreur me disant que je tente de free quelque chose qui n'est pas alloué...
"pointer being freed was not allocated"
Voici mon code:
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
|
#include <stdio.h>
#include <stdlib.h>
void print_tab(char **tab)
{
int i;
i = 0;
while (tab[i])
{
printf("%s\n", tab[i]);
i++;
}
}
char **fill_tab(char **tab)
{
tab[0] = "str1";
tab[1] = "str2";
return (tab);
}
char **malloc_tab(char **tab, int nb_str, int nb_char)
{
int i;
int j;
i = 0;
tab = malloc(sizeof(char *) * nb_str);
tab[nb_str - 1] = 0;
while (tab[i])
{
tab[i] = malloc(sizeof(char) * nb_char);
i++;
}
return (tab);
}
void free_tab(char **tab)
{
int i;
i = 0;
printf("%s", tab[2]);
while (tab[i])
{
printf("tab[%d]", i);
free(tab[i]);
i++;
}
free(tab);
}
int main(int argc, char **argv)
{
char **tab;
tab = malloc_tab(tab, 3, 6);
tab = fill_tab(tab);
print_tab(tab);
free_tab(tab);
return (0);
} |
Merci pour votre aide !