pb d'allocation de mémoire
	
	
		bonjour tout le monde,
j'ai un soucis d'allocation dynamique de mémoire. Ca se présente comme ça :
j'ai un struct 
	Code:
	
1 2 3 4 5 6 7 8
   | struct element{
       float **tab;
       int taille;
       float distance;
 
       int choix[2];
 
       }; | 
 tab représentera, une fois alloué, un tableau carré de taille "taille"
j'ai aussi fait une fonction pour allouer de la mémoire à mon **tab
	Code:
	
1 2 3 4 5 6 7
   | void creation_tab(float **t, int n){
     t=(float**)malloc(n*sizeof(float*));
     if (!t) printf("oups");
     short i;
     for (i=0; i<n; i++)
         {t[i]=(float*)malloc(n*sizeof(float));     if (!t[i]) printf("oupss"); }
} | 
 que j'appelle de la manière suivante : 
	Code:
	
1 2
   | struct element debut;
creation_tab(debut.tab,5);  | 
 jusque là, ça va, mais dès que je fais un accès à un élément de mon **tab en lecture ou en écriture 
	Code:
	
debut.tab[0][0]=10;
  j'ai une erreur d'exécution. J'ai beau retourner le problème dans tous les sens, je ne vois
 pas l'erreur.
	 
	
	
	
		Re: pb d'allocation de mémoire
	
	
		
	Citation:
	
		
		
			
				Envoyé par shura
				
			
			j'ai un soucis d'allocation dynamique de mémoire. Ca se présente comme ça :
			
		
	 
 C'est pas un problème d'allocation, mais un problème de paramètres. 
Rappel : les passage de paramètres se font uniquement par valeur en C.
	Citation:
	
		
		
			
	Code:
	
void creation_tab(float **t, int n){
 <...>
	Code:
	
1 2
   | struct element debut;
creation_tab(debut.tab,5);  | 
 
			
		
	 
 L'erreur est courante.
Pour qu'une fonction modifie la valeur d'une variable, il faut passer l'adresse de cette variable
	Code:
	
1 2
   | struct element debut;
creation_tab(&debut.tab,5);  | 
 pour ça, il faut un pointeur du bon type :
	Code:
	
void creation_tab(float ***t, int n){
 et modifier la fonction en conséquence. C'est horriblement lourd et une meilleure approche est d'utiliser une structure avec un constructeur (et un destructeur)
	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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
   |  
#include <stdio.h>
#include <stdlib.h>
 
#define DBG 0                   /* 0 1 2 3 */
 
struct mat2
{
   float **tab;
   size_t taille;
};
 
struct element
{
   struct mat2 *mat;
   float distance;
 
   int choix[2];
};
 
void mat2_delete (struct mat2 *this)
{
   if (this != NULL)
   {
      if (this->tab != NULL)
      {
         size_t i;
         for (i = 0; i < this->taille; i++)
         {
            free (this->tab[i]), this->tab[i] = NULL;
         }
 
         free (this->tab), this->tab = NULL;
      }
      free (this), this = NULL;
   }
}
 
struct mat2 *mat2_create (size_t n)
{
   /* allocates the object */
   struct mat2 *this = malloc (sizeof *this);
 
#if DBG==1
   free (this), this = NULL;
#endif
   if (this != NULL)
   {
      this->taille = n;
      this->tab = NULL;
 
      {
         /* allocates the array of pointers */
         this->tab = malloc (sizeof *this->tab * n);
#if DBG==2
         free (this->tab), this->tab = NULL;
#endif
         if (this->tab != NULL)
         {
            size_t i;
 
            for (i = 0; i < n; i++)
            {
               this->tab[i] = NULL;
            }
 
            /* allocates the array of data */
            for (i = 0; i < n; i++)
            {
               this->tab[i] = malloc (n * sizeof *this->tab[i]);
#if DBG==3
               free (this->tab[i]), this->tab[i] = NULL;
#endif
 
 
               if (this->tab[i] == NULL)
               {
                  mat2_delete (this), this = NULL;
                  break;
               }
            }
         }
         else
         {
            mat2_delete (this), this = NULL;
         }
      }
   }
   return this;
}
 
int main (void)
{
   struct element debut =
   {0};
 
   debut.mat = mat2_create (5);
   if (debut.mat != NULL)
   {
      debut.mat->tab[0][0] = 10;
 
      mat2_delete (debut.mat), debut.mat = NULL;
   }
   else
   {
      printf ("memory error (DBG=%d)\n", DBG);
   }
   return 0;
} |