Réallocation d'espace d'une grille

Résolu
heseinberg Messages postés 1 Date d'inscription jeudi 6 avril 2017 Statut Membre Dernière intervention 6 avril 2017 - Modifié le 7 avril 2017 à 11:01
cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 - 8 avril 2017 à 19:06
il s'agit d'une tableau de caractère dans lequel je veux insérer des mots, seulement lorsque la position de la case choisie par l'utilisateur n'est pas comprise dans les tailles du tableaux (long,large) alors on fait une realloc afin d'agrandir celui ci mais mon code ne fonctionne pas:

char*** reallocation(char ***tableau, int *taillex, int *tailley)
{
    tableau = NULL;
    tableau = (char***)realloc(tableau, *taillex * sizeof (char *));

    int i, j;
    for (i=0; i< *tailley; i++)
    {
        tableau[i] = (char**)realloc(tableau, *tailley * sizeof(char * ));

        for (j=0; i<*tailley ; j++)
        {
            tableau[i][j] = (char *) realloc(tab, 0 * sizeof(char) );

            tableau[i][j][0] = '*';
        }
    }

    return tableau;

}

void insererCharDansTab2Dv2 (char ***tab, int *taillex, int *tailley, int posx, int posy, char c)
{
    if (posx > *taillex)
    {
        taillex = &posx;
        reallocation(tab, taillex, tailley);

    }

    if (posy > *tailley)
    {
        tailley = &posy;
        reallocation(tab, taillex, tailley);

    }

    if ((posx<=*taillex)&&(posy<=*tailley))
    {
        tab[posx-1][posy-1][0] = c;
        afficherTab2D(tab, *taillex, *tailley);
    }
}


void insererMotDansTab2Dv2 (char ***tab, int *taillex, int *tailley, int posx, int posy, int horizontal, char *mot)
{
    if (posx > *taillex)
    {
        taillex = &posx;
        reallocation(tab, taillex, tailley);
    }

    if (posy > *tailley)
    {
        tailley = &posy;
        reallocation(tab, taillex, tailley);
    }


    if (*mot != '\0')
    {
        insererCharDansTab2Dv2(tab, taillex, tailley, posx, posy, *mot);
        if (horizontal == FLAG_HORIZONTAL) insererMotDansTab2Dv2(tab, taillex, tailley, posx, posy+1, horizontal, ++mot);
        if (horizontal == FLAG_VERTICAL) insererMotDansTab2Dv2(tab, taillex, tailley, posx+1, posy, horizontal, ++mot);
    }

}

1 réponse

cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 123
Modifié le 8 avril 2017 à 19:11
Bonjour.

Tu as deux souci majeurs:
  • Tu essaies d'allouer une taille de tableau puis de décrémenter tes index (x et y). Il faut faire l'inverse. Laisser l'utilisateur choisir ses index, et penser à prendre taille + 1 quand on (re-)alloue.
  • Tu considères que la taille en y est la même partout. Ce qui n'est pas vrai. Tu peux avoir des lignes de mots de différentes tailles.


La seule manière de s'en sortir proprement, c'est de stocker la taille de chacune de tes lignes séparément. On peut le faire via une structure qui contiendrait la ligne (char**) et sa taille (int).

Pour reprendre ton idée, avec des structures, ça donnerait ceci:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
  unsigned int size;
  char** words;
} Line;

typedef struct
{
  unsigned int size;
  Line** lines;
} Tab;

void show_words(const Line* line)
{
  unsigned int i = 0;

  if (!line || line->size == 0)
    printf("(empty)");
  else
    for (i = 0; i < line->size; ++i)
      printf("[%s]", line->words[i] ? line->words[i] : "");

  printf("\n");
}

void show_tab(const Tab* tab)
{
  unsigned int i = 0;

  if (!tab || tab->size == 0)
    printf("tab is empty!");
  else
    for (i = 0; i < tab->size; ++i)
      show_words(tab->lines[i]);
}

Line* insert_word_into_line(Line* line, unsigned int y, const char* word)
{
  unsigned int i = 0;
  const unsigned int size_word = !word ? 0 : strlen(word);

  if (!line)
  {
    line = malloc(1 * sizeof (Line));
    line->words = NULL;
    line->size = 0;
  }

  if (y >= line->size)
  {
    line->words = realloc(line->words, (y + 1) * sizeof (char*));
    for (i = line->size; i < y + 1; ++i)
      line->words[i] = NULL;
    line->size = y + 1;
  }

  if (line->words[y] == NULL || strlen(line->words[y]) < size_word)
    line->words[y] = realloc(line->words[y], (size_word + 1) * sizeof (char));

  for (i = 0; i < size_word; ++i)
    line->words[y][i] = word[i];
  line->words[y][size_word] = 0;

  return line;
}

Tab* insert_word_into_tab(Tab* tab, unsigned int x,
                          unsigned int y, const char* word)
{
  unsigned int i = 0;

  if (!tab)
  {
    tab = malloc(1 * sizeof (Line));
    tab->lines = NULL;
    tab->size = 0;
  }

  if (x >= tab->size)
  {
    tab->lines = realloc(tab->lines, (x + 1) * sizeof (Line*));
    for (i = tab->size; i < x + 1; ++i)
      tab->lines[i] = NULL;
    tab->size = x + 1;
  }

  tab->lines[x] = insert_word_into_line(tab->lines[x], y, word);

  return tab;
}

void free_line(Line* line)
{
  unsigned int i = 0;

  if (!line)
    return;

  for (i = 0; i < line->size; ++i)
    free(line->words[i]);
  free(line->words);
  free(line);
}

void free_tab(Tab* tab)
{
  unsigned int i = 0;

  if (!tab)
    return;

  for (i = 0; i < tab->size; ++i)
    free(tab->lines[i]);
  free(tab->lines);
  free(tab);
}

int main(void)
{
  Tab* tab = 0;

  tab = insert_word_into_tab(tab, 0, 0, "coucou");
  tab = insert_word_into_tab(tab, 2, 0, "toto");
  tab = insert_word_into_tab(tab, 0, 2, "world");
  tab = insert_word_into_tab(tab, 3, 3, "un petit test");
  show_tab(tab);
  free_tab(tab);
  tab = 0;

  tab = insert_word_into_tab(tab, 1, 1, "Un autre");
  tab = insert_word_into_tab(tab, 2, 2, "test");
  tab = insert_word_into_tab(tab, 3, 3, "en");
  tab = insert_word_into_tab(tab, 4, 4, "diagonal");
  show_tab(tab);
  free_tab(tab);

  return 0;
}


PS: Attention, je vois du malloc "casté" dans ton code. Il n'est pas nécessaire de caster un malloc en C, uniquement en C++. Si tu utilises un compilateur C++ pour faire du C, tu peux avoir des soucis. Le C et le C++ ne sont pas compatibles à 100%. Il y a des fonctionnalités en C qui n'existent pas en C++ ou qui réagissent différemment !


Améliorer votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
0
Rejoignez-nous