Pb de malloc

Bonjour, j'ai un pb de malloc. Voici tout d'abord ce que j'ai fait :

typedef struct t_mat
{
int dim;
double * data;
} Tableau;

void alloc_Tableau(Tableau *t,unsigned int dim_)
{
assert(dim_>0);
t->dim=dim_;
t->data=malloc(dim_*sizeof(double));
assert(t->data!=NULL);
int i;
for(i=0;i<t->dim;i++)
t->data[i]=0.;
return ;
}

void free_Tableau(Tableau * t)
{
free(t->data);
t->data=NULL;
return ;
}

void affiche_Tableau(Tableau t)
{
if(t.data==NULL)
printf("\nTableau inexistant\n");
else
{
int i;
for(i=0;i<t.dim;i++)
printf("%3.5g\t",t.data[i]);
printf("\n");
}

return;
}

Dans le main je fais la chose suivante :

Tableau t1;
alloc_Tableau(&t1,3);
affiche_Tableau(t1);
free_Tableau(&t1);

et là j'obtiens bien la sortie : 0 0 0

Maintenant si je fais

Tableau * t2
alloc_Tableau(t2,3);

et bien j'ai une erreur de segmentation. Savez-vous pourquoi ?

Merci beaucoup.

3 réponses

  1. Bonjour j'ai trouvé mon erreur :
    avant je faisais :

    Tableau * t2;
    alloc_Tableau(t2,3);

    mais la solution qui marche est

    Tableau * t2=malloc(sizeof(Tableau));
    alloc_Tableau(t2,3);

    Merci de votre aide.
    1. Modérateur
      alloc_Tableau(&t2, 3, 3.14159);
      As tu effectué cet appel?
      Quel message d'erreur?
      Quel compilateur?
      J'ai testé sous Debian et cygwin avec succès.
      Par contre, le compilateur Microsoft me jette
      [G:\cygwin\home\root\src\C\tableau]cl /c /TC tableau.c
      Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
      Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
      
      tableau.c
      tableau.c(18) : error C2143: syntax error : missing ';' before 'type'
      tableau.c(19) : error C2065: 'i' : undeclared identifier
      

      Mais je ne suis pas spécialiste de ce compilo...
      Johan
      1. Modérateur
        Vérifie ton appel à alloc_Tableau...
        johand@horus:~/src/c/tableau$ cat tableau.c
        #include <stdlib.h>
        #include <stdio.h>
        #include <assert.h>
        typedef struct t_mat
        {
          int dim;
          double * data;
        } Tableau;
        
        
        void alloc_Tableau(Tableau *t,unsigned int dim_, double val)
        {
          assert(dim_>0);
          t->dim=dim_;
          t->data=malloc(dim_*sizeof(double));
          assert(t->data!=NULL);
          int i;
          for(i=0;i<t->dim;i++)
            t->data[i]=val;
          return ;
        }
        
        void free_Tableau(Tableau * t)
        {
          free(t->data);
          t->data=NULL;
          return ;
        }
        
        void affiche_Tableau(Tableau t)
        {
          if(t.data==NULL)
            {
              printf("\nTableau inexistant\n");
            }
          else
            {
              int i;
              for(i=0;i<t.dim;i++)
        	printf("%3.5g\t",t.data[i]);
              printf("\n");
            } 
        }
        
        void main(void)
        {
          Tableau  t2;
          alloc_Tableau(&t2, 3,  3.14159);
          affiche_Tableau(t2);
          free_Tableau(&t2);
        }
        
        johand@horus:~/src/c/tableau$ touch tableau.c ; make tableau; ./tableau
        cc     tableau.c   -o tableau
        tableau.c: In function 'main':
        tableau.c:46: warning: return type of 'main' is not 'int'
        3.1416  3.1416  3.1416
        

        Cela compile et tourne, non ?
        1. Salut, merci de ta réponse. Effectivement, c'est plus malin de rajouter un 3e argument à alloc_Tableau.
          Avant je faisais du c++. J'ai essayé de faire
          void alloc_Tableau(Tableau * t,unsigned intN,double val=0)

          et ensuite de faire
          Tableau t1;
          alloc_Tableau(&t1,3) (le 3e argument vaut par défaut 0).

          J'ai l'impression que ca ne marche pas en C. Le compilo m'a gentilement jeté.

          Merci.