Thread: double free or corruption (out) when assigning a pointer inside a struct.

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    1

    double free or corruption (out) when assigning a pointer inside a struct.

    Hi, I'm having troubles with the following code:

    Code:
    #include "jugador.h"
    struct JugadorStruct_t {
        int fichas, manos_ganadas, manos_perdidas, manos_empatadas;
        char* nombre;
        int* fichas_partidas;
    };
    
    
    int initJugador(JugadorPtr_t jugador) {
        //PRE: si jugador.txt existe, tiene al menos 4 filas
        //POST: si 1, el puntero apunta a un jugador inicializado con el fichero.
        //si 0, error abriendo el archivo
    FILE* fp = fopen("/home/norhther/CLionProjects/blackjack/jugador.txt","r");
    
        if (fp == NULL) {
            printf("El archivo del jugador no existe\n");
            fclose(fp);
            return 1;
        }
        else {
            char* line = NULL;
            size_t len = 0;
    
            getline(&line, &len, fp);
            jugador->nombre = strdup(line);
    
            getline(&line, &len, fp);
            jugador->fichas = atoi(line);
    
            getline(&line, &len, fp);
            jugador->manos_ganadas = atoi(line);
    
            getline(&line, &len, fp);
            jugador->manos_perdidas = atoi(line);
    
            getline(&line, &len, fp);
            jugador->manos_empatadas = atoi(line);
    
            int manos = jugador->manos_perdidas + jugador->manos_ganadas + jugador->manos_empatadas;
            int *p = ((int*)malloc(manos*sizeof(int)));
            jugador->fichas_partidas = p;
    
            int i;
            for (i = 0; i < manos; i++) {
                getline(&line, &len, fp);
                p[i] = atoi(line);
            }
    
        }
    
        fclose(fp);
        return 0;
    }
    jugador->fichas_partidas = p is giving me this error, and I don't know why
    it is happening. Any clues?

    Thanks!

    Edit: I found the following:
    in my jugador.h file I've got the following definition
    Code:
    typedef struct JugadorStruct_t * JugadorPtr_t;
    


    and I allocate the space for this in my main like this:

    [
    CODE]
    JugadorPtr_t jugador = malloc(sizeof jugador);
    [/CODE]

    I tried
    JugadorPtr_t jugador = malloc(sizeof jugador * 50) and now I don't get the error. Any clue?


    Last edited by norhther; 08-12-2018 at 03:45 PM.

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    393
    This is wrong since you only assign enough space for a pointer to the struct, not the struct itself:
    Code:
    typedef struct JugadorStruct_t * JugadorPtr_t;
    
    JugadorPtr_t jugador = malloc(sizeof jugador);
    Multiplying it by 50 assigns more than is needed.

    The proper thing is to do the following. I also got rid of the pointer typedef since that's generally considered to be a bad idea (hiding the pointer). And I added a typedef to the struct so you don't have to say "struct" all over the place.

    Code:
    // This should be in judador.h:
    typedef struct JugadorStruct_t {
        int fichas, manos_ganadas, manos_perdidas, manos_empatadas;
        char* nombre;
        int* fichas_partidas;
    } Jugador_t;
    
    int initJugador(Jugador_t *jugador);  // make the pointer explicit in the function
    
    
    // Then in your main you can either do this:
    Jugador_t *jugador = malloc(sizeof *jugador);
    
    initJugador(jugador);
    
    // Then at some point
    free(jugador);
    
    
    
    // Or even this
    Jagador_t jugador;  // make a struct instead of a pointer to a struct
    
    initJugador(&jugador);  // pass the address of the struct
    
    // Then you don't need the free.
    You should also free(line) before you return from initJugador since getline will have allocated it and it's your responsibility to free it.
    Last edited by john.c; 08-12-2018 at 04:11 PM.
    Simplicity is the ultimate sophistication.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double free or corruption
    By MaTi in forum C Programming
    Replies: 4
    Last Post: 01-24-2012, 05:07 PM
  2. Help: double free corruption??
    By GOGO1104 in forum C Programming
    Replies: 7
    Last Post: 09-28-2010, 01:12 PM
  3. Assigning to double pointer in a struct*
    By Trenthan in forum C Programming
    Replies: 8
    Last Post: 10-20-2009, 05:29 AM
  4. double free or corruption (out)
    By Mouser58907 in forum C Programming
    Replies: 5
    Last Post: 02-25-2009, 12:20 AM
  5. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM

Tags for this Thread