简体   繁体   中英

C - malloc or fgets, I don't know what's not working

Here's my problem : I've a 2d char matrix wich I malloc with a function. Afterward, I want to get a map from a file, but I've got a segmentation fault right there and I dont know why... Here's a code sample :

// struct where I put the map and others informations from the  
typedef struct problem_t
{
    char *nom;
    Coordonnees arrivee, depart;
    int nb_ligne, nb_col;
    char **     
} Problem;

// Function wich malloc the map
int mallocCarte( char *** carte, int nbLigne, int nbCol )
{
    *carte = malloc( nbLigne * sizeof( char* ) );

    if ( *carte == NULL )
    {
        return false;
    }

    int i;
    for ( i = 0; i < nbLigne ; ++i )
    {

        (*carte) [i] = malloc( nbCol * sizeof( char ) );
        if ( (*carte) [i] == NULL )
        {
            return false;
        }
    }

    return true;

} // mallocCarte  ()

// Code sample, I've already got the others informations, now, I'd like to get the map
// On commence par reserver l'espace memoire reserve à la carte.
int res = mallocCarte( &problem->carte, problem->nb_ligne, problem->nb_col );

// Si l'allocation s'est mal passée, on renvoie un message
if ( res == false )
{
    exc.num_Exc = MALLOC_ERROR;
    exc.msg_Err = "Erreur lors de l'allocation mémoire de la carte";
    return exc;
}

printf( "Recuperation de la carte 2 ...%d %d\n", problem->nb_ligne,
        problem->nb_col );
int nbLi = 0;
int nbCol = 0;
while ( fgets( fromFile, 1, file ) != NULL && nbLi < problem->nb_ligne )
{
    if ( fromFile [0] == '\n' )
    {
        nbCol = 0;
        ++nbLi;
        continue;
    }

    if ( nbCol == problem->nb_col )
    {
        printf( "malformed input file!\n" );
        exit( -1 );
    }


    ( problem->carte ) [nbLi] [nbCol++] = fromFile [0];
}

It's been many days and I really don't know what to do... I'd be so greatful If someone could help me !

Thanks you

(Here is the source file where I take informations. First they are problem name, then some coordinates, and finally the map size. At the end of the file is the map https://dl.dropbox.com/u/56951442/map.txt )

When you increment nbLi in main , you need to reset nbCol to zero. You are allowing the column to continue to increase infinitely and exceeding the size of your array.

Also, you should not cast malloc in C .

In addition, you should be passing &problem->carte into your allocation function...

// Function wich malloc the map
int mallocCarte( char *** carte, int nbLigne, int nbCol )
{
    *carte = malloc( nbLigne * sizeof( char* ) );
...
    (*carte)[i] = malloc( nbCol * sizeof( char ) );
...
}

main()
{
   ...
   int res = mallocCarte( &problem->carte, problem->nb_ligne, problem->nb_col );
   ...
}

You should probably add a test, too, to ensure you don't wander off the end of the column if your input file is illformed...

   if ( isspace( fromFile [0] ) )
   {
       nbCol = 0;
       ++nbLi;
       continue;
   }

   if( nbCol == problem->nb_col )
   {
       printf( "malformed input file!\n" );
       exit( -1 );
   }

Are you sure you mean to be using fgets( fromFile, 1, file ) ?? The below description implies that fgets() is always returning an empty string until the end of the file and fromFile[0] will always be '\\0' until EOF . You should be using fgets( fromFile, 2, file )

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\\0' is stored after the last character in the buffer.

problem is a object of structure problem_t. It is not a pointer, you should access structure variable like

problem.carte,
problem.nb_ligne
problem.nb_col

I tried your code on my system. your code is working fine for me with your given map.txt. I just declared char fromFile[2]; While(fgets(fromFile,2,file) != NULL) {//printing file}. Its printing file properly for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM