Problème de mémoire
panderan
-
panderan -
panderan -
Bonjour,
Je comprend pas pourquoi quand demande d'afficher mon tableau a double dimension il m'affiche jusqu'a 97 ligne alors que normalement je lui alloue que 11 de place pour chaque ligne.
Donc je voudrais savoir comment faire pour lui allouer juste 10 colonnes avec 11 caracteres par colonnes.
Voila mon programme
Je comprend pas pourquoi quand demande d'afficher mon tableau a double dimension il m'affiche jusqu'a 97 ligne alors que normalement je lui alloue que 11 de place pour chaque ligne.
Donc je voudrais savoir comment faire pour lui allouer juste 10 colonnes avec 11 caracteres par colonnes.
Voila mon programme
void mapdup(char *map, const char *str, int height)
{
int cpt;
cpt = 0;
while (cpt < height)
{
map[cpt] = str[cpt];
cpt = cpt + 1;
}
map[cpt] = '\0';
}
char **recup_map(char *filename)
{
t_bunny_ini *ini;
const char *wid;
const char *heig;
char **map;
const char *str;
int cnt;
int width;
int height;
ini = bunny_load_ini(filename);
if (ini == NULL)
exit(84);
wid = bunny_ini_get_field(ini, "level1", "width", 0);
heig = bunny_ini_get_field(ini, "level1", "height", 0);
height = atoi(heig);
width = atoi(wid);
cnt = 0;
while (cnt < height)
{
map[cnt] = malloc(sizeof(char) * (width + 1));
if (map[cnt] == NULL)
exit(1);
cnt = cnt + 1;
}
cnt = 0;
str = malloc(sizeof(char) * (height + 1));
if (str == NULL)
exit(1);
cnt = 0;
printf("cnt = %d\n", cnt);
while ((str = bunny_ini_get_field(ini, "level1", "data", cnt)) != NULL)
{
printf("str = %s\n", str);
mapdup(map[cnt], str, width);
cnt = cnt + 1;
}
my_putstr("map[0] = ");
my_putstr(map[0]);
my_putchar('\n');
bunny_delete_ini(ini);
return (map);
}
int main()
{
char **map;
int cnt;
map = recup_map("map1.ini");
while (cnt < 10)
{
printf("map[%d] = %s\n", cnt, map[cnt]);
cnt = cnt + 1;
}
}
| EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ici : ICI Merci d'y penser dans tes prochains messages. |
1 réponse
-
Salut,
Le problème vient peut-être du fait que tu ne fait aucune allocation mémoire pour map et que tu ajoutes des éléments au tableau.
Pareil pour la variable cnt dans ton main, initialises-là à 0
comment faire pour lui allouer juste 10 colonnes avec 11 caracteres par colonnes ?
map = malloc(sizeof(char*)*10); if(map == NULL){ printf("Problème allocation mémoire\n"); return EXIT_FAILURE; } int cnt; for(cnt=0;cnt<10;cnt++){ map[cnt]=malloc(sizeof(char)*11); if(map[cnt]==NULL){ printf("Problème allocation mémoire\n"); return EXIT_FAILURE; } }
Qui ne tente rien n'a rien