Segmentation fault + fprinf

Alexandre -  
 alexandre -
Bonjour,

Voila j'ai developpé un petit bout de code est tout me laisse a penser que la fonction fprintf, qui me permet d'ecrire une liste de coordonnées dans un fichier.dat (environ 800*2 coordonnées), creer un probleme de segmentation fault.

En fait apres compilation (sans probleme apparement) soit l'execution se fait sans probleme et le fichier est correctement ecrit, soit le progamme s'arrete a la premiere iteration.
Lorsque j'enleve cette fonction fprintf, il n'y a plus aucun probleme de segmentation fault et les coordonnées sont bien determinés.

Merci d'avance aux ames charitables,

A+

Alex

2 réponses

  1. jisisv Messages postés 3678 Statut Modérateur 936
     
    Un petit extrait de ton code pourrait nous aider à résoudre le problème

    ...and the software said "Requires Windows98, Win2000, or better
    So I installed Unix.
    0
  2. alexandre
     
    Voici le bout de programme qui me pose probleme, c'est un peu long
    aussi merci aux courageux!!

    #include <math.h>
    #include <string.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    #define ran() (float) rand()/RAND_MAX

    /*Definition d'une structure qui contient les proprietes
    necessaires au modele*/

    typedef struct toto {

    float Q_TRUNC;
    float SIG_ALPHA;
    float SIG_BETA;
    float H_CENTER;
    float SIG_HK;
    float SIG_HI;
    float kx;

    float norm;
    float RATIO;
    float MSAT;
    float ANISOTROPIE;

    int tdim;
    int npom;

    float *hi_tab;
    float *hk_tab;
    float *ptab;

    float *alpha;
    float *beta;

    float *ux;
    float *uy;
    float *uz;

    char *etat;

    } sw_model;

    /*Allocation de la memoire dynamique necessaire aux tableaux de
    vecteurs orientant les operateurs de SW*/

    int create_model( sw_model *pmodel )
    {
    int tmax;

    tmax= pmodel->npom;

    pmodel->hi_tab = ( float* ) calloc( tmax, sizeof(float));
    pmodel->hk_tab = ( float* ) calloc( tmax, sizeof(float));
    pmodel->ptab = ( float* ) calloc( tmax, sizeof(float));

    pmodel->alpha = (float*) calloc( tmax, sizeof(float));
    pmodel->beta = (float*) calloc( tmax, sizeof(float));
    pmodel->ux = (float*) calloc( tmax, sizeof(float));
    pmodel->uy = (float*) calloc( tmax, sizeof(float));
    pmodel->uz = (float*) calloc( tmax, sizeof(float));

    pmodel->etat = (char*) calloc( tmax, sizeof(char));

    return(1);

    }
    /* Procedure de renseignement sur les proprietes intrinseques du
    materiau*/

    int read_material( sw_model *model, char *name )
    {
    FILE *dat;
    float value;
    int vnr;
    char var_name[20], *pos;
    char line[80];
    static char var_list[] = "sig_alpha ,sig_beta ,anisotropie ,sig_hi ,sig_hk ,h_center ,m_saturation ,truncation ,kx ,tdim ,npom ";

    printf("\nlecture dans le fichier materiau...\n\n");

    dat = fopen( name, "r" );

    if ( dat==0)
    {
    printf("echec ouverture\n");
    exit (1);
    }
    while( fgets( line, 80, dat ) )
    {
    if ( line[0] == '#' || line[0] == '\n' ) continue;
    else
    {
    sscanf(line, "%s%*s%f", var_name, &value);
    pos = strstr( var_list, var_name );
    vnr = ( int ) ( pos - var_list )/15;

    printf("%s", line);

    switch( vnr )
    {

    case 0: model->SIG_ALPHA = value;
    break;
    case 1: model->SIG_BETA = value;
    break;
    case 2: model->ANISOTROPIE = value;
    break;
    case 3: model->SIG_HI = value;
    break;
    case 4: model->SIG_HK = value;
    break;
    case 5: model->H_CENTER = value;
    break;
    case 6: model->MSAT = value;
    break;
    case 7: model->Q_TRUNC = value;
    model->RATIO = pow((1.-value)/(1.-pow(value,1./3)),1.5);
    break;
    case 8: model->kx = value;
    break;
    case 9: model->tdim = (int) value;
    break;
    case 10: model->npom = (int) value;
    break;

    }
    }

    }

    fclose( dat );

    }

    /*calcul de la densite de Preisach*/

    float pr_dens(float H_CENTER, float SIG_HI,float SIG_HK, float hi, float hc )
    {
    return( exp( -0.5*hi*hi/(SIG_HI*SIG_HI)) * exp( -0.5*(hc-H_CENTER)*(hc-H_CENTER)/(SIG_HK*SIG_HK)) );
    }

    /*routine de choix aleatoire de valeur*/

    double ran_gauss()
    {
    double r=0.0;
    int i;

    for( i=0; i<=100; i++ )
    r += ( double ) rand()/RAND_MAX - 0.5;

    return( r/2.9 );
    }

    /*routines de changement de repere*/

    void trans_o3x( float x, float y, float z,
    float *xt, float *yt, float *zt, float a )
    {
    *xt= x;
    *yt= cos(a)*y - sin(a)*z;
    *zt= sin(a)*y + cos(a)*z;
    }

    void trans_o3y( float x, float y, float z,
    float *xt, float *yt, float *zt, float a )
    {
    *yt= y;
    *xt= cos(a)*x - sin(a)*z;
    *zt= sin(a)*x + cos(a)*z;
    }

    /*Procedure de repartition aleatoire des vecteurs "axe facile" des operateurs de
    Stoner-Wohlfarth et des couples (hi,hc) des points du plan de Preisach: la routine
    qui me pose probleme: segmentation fault pour npom~800*/

    void dispersion( sw_model *pmodel )
    {
    int i,nb_ope;
    FILE *dat1, *dat2;
    float x,y,z,x0,y0,z0;
    float a, b, beta, alpha, norm;
    nb_ope=pmodel->npom;
    printf("nbre d'operateurs:%d\n",nb_ope);

    dat1= fopen("/home/perso/fbernard/alex_volatier/Essais/verifications/resultat/nouveau1.dat","wb");

    if ( dat1==0)
    {
    printf("echec ouverture de disb.dat\n");
    exit (1);
    }

    printf("Creation d'une distribution spatiale...\n");

    /*sorte de reinitialisation du generateur de nombre aleatoire*/

    srand( (unsigned)time( NULL ) );

    for( i=0; i <pmodel->npom;i++)
    {

    alpha= pmodel->SIG_ALPHA*ran_gauss()*3.1415/180;
    beta= pmodel->SIG_BETA*ran_gauss()*3.1415/180;

    x0= cos(beta);
    z0= sin(beta);
    y0=0.0 ;

    /*engendrer la rotation autour de x*/

    trans_o3x( x0, y0, z0, &x, &y, &z, alpha );

    /* Creer Anisotropie */

    trans_o3y( x, y, z, &pmodel->ux[i], &pmodel->uy[i],&pmodel->uz[i], pmodel->ANISOTROPIE );

    pmodel->alpha[i]=alpha ;
    pmodel->beta[i] =beta ;

    fprintf( dat1, "%d %f %f %f \n",i,pmodel->ux[i], pmodel->uy[i], pmodel->uz[i]);

    fflush(dat1);

    /*printf( "ligne:%d,ux:%f uy:%f uz:%f\n",i,pmodel->ux[i],pmodel->uy[i], pmodel->uz[i]);*/

    }
    printf("...done\n\n");

    fclose( dat1 );

    /* distribution de Preisach*/

    dat2= fopen("/home/perso/fbernard/alex_volatier/Essais/verifications/resultat/nouveau2.dat","wb");

    if ( dat2==0)
    {
    printf("echec ouverture de pdisb.dat\n");
    exit (1);
    }

    printf("Creation de la distribution de Preisach...\n");

    srand( (unsigned)time( NULL ) );
    for( i=0; i<pmodel->npom; i++ )
    {

    do
    {

    pmodel->hi_tab[i]= 3*pmodel->SIG_HI*( 2*ran()-1 );

    printf("Creation de hi:%f...\n",pmodel->hi_tab[i]);
    pmodel->hk_tab[i]= 3*pmodel->SIG_HK*( 2*ran()-1)+pmodel->H_CENTER;
    printf("Creation de hk:%f...\n",pmodel->hk_tab[i]);
    pmodel->ptab[i]=pr_dens( pmodel->H_CENTER, pmodel->SIG_HI,pmodel->SIG_HK, pmodel->hi_tab[i], pmodel->hk_tab[i] );
    printf("Calcul de pdens:%f...\n\n",pmodel->ptab[i]);
    printf("Calcul de limite:%f...\n\n",exp(-9/2));
    }
    while( pmodel->ptab[i] < exp(-3*3/2 ));

    printf("couple %d OK!!\n",i);

    fflush(dat2);

    printf("couple %d OK!!\n",i+1);
    fflush(dat2);
    /*segmentation ici quelque fois!!*/
    fprintf( dat2, "%f %f \n", pmodel->hi_tab[i], pmodel->hk_tab[i] );
    fflush(dat2);
    printf("couple %d OK!!\n",i+2);

    pmodel->hk_tab[i] *= pmodel->RATIO;
    }

    printf("...done\n\n");

    fclose( dat2 );
    }

    void liberation(sw_model *pmodel)
    {
    free(pmodel->hi_tab);
    free(pmodel->hk_tab );
    free(pmodel->ptab );

    free(pmodel->alpha );
    free(pmodel->beta );
    free(pmodel->ux );
    free(pmodel->uy );
    free(pmodel->uz );

    free(pmodel->etat );

    }

    void main(int argc,char *argv[])
    {
    int i;
    sw_model *model; /*definition d'un pointeur sur la
    structure*/
    model= (sw_model*) calloc(1,sizeof(sw_model)); /*allocation dynamique de la memoire */

    if(create_model(model)==0) printf("Memoire insuffisante\n"); /*appel de l'allocation des tableaux de
    model*/
    else printf("creation model termine\n");

    read_material(model,argv[1]);

    dispersion(model);

    liberation(model);

    free(model);
    }

    Merci encore aux ames charitables et surtout patientes !

    A+

    Alexandre
    0