Nico Passaglia
Nico Passaglia

Reputation: 57

Problems with memory managment in C

I have the following C function, which is causing me a lot of problems with memory, if I dont free the memory it works fine but when I call another function i get a malloc memory corrupted error, but I free it I get free invalid pointer. And finally when i define new pointers for fecha and fechaVariable due to the fact that strtok works weird i get corrupted_size vd prev_size error. Can someone help me. Here is my code:

void mensual_precipitacion(struct fila *arregloArchivo, char *precipitacion, int estacion){
    float acumulado = 0.00;
    char *fecha;
    char *fechaVariable;

    int i;
    int boolIncorrecto = 1;
    char *mes;
    char *anio;
    char *lluvia;   
    int boolfecha = 1;

    fecha = malloc(1000*sizeof(char));
    fechaVariable = malloc(1000*sizeof(char));
    lluvia = malloc(1000*sizeof(char));



    for(i=0;i<cantDatos;i++){

        if(arregloArchivo[i].numero != estacion){
            continue;
        }
        boolIncorrecto =0; 
        if(boolfecha){
            strcpy(fecha,arregloArchivo[i].fecha);  
            boolfecha = 0;

            fecha = strtok(fecha,"/");

            mes = strtok(NULL,"/");
            anio = strtok(NULL," ");
        }

        strcpy(fechaVariable,arregloArchivo[i].fecha);

        fechaVariable = strtok(fechaVariable,"/");

        fechaVariable = strtok(NULL, "/");

        if(strcmp(mes,fechaVariable) == 0){
            acumulado += arregloArchivo[i].precipitacion;

        }else{

            sprintf(lluvia,"%s/%s:%f[mm]\n",mes,anio,acumulado);
            strcat(precipitacion,lluvia);
            acumulado = 0.00;
            boolfecha = 1;
            memset(lluvia,'\0',sizeof(lluvia));
        }

    }
    if(boolIncorrecto){
        strcpy(lluvia,"Nro de estacion inexistente");
    }else{
        sprintf(lluvia,"%s/%s:%f[mm]\n",mes,anio,acumulado);
    }
    //
        //printf("%s\n",lluvia );
    strcat(precipitacion,lluvia);

    free(fecha);
    free(fechaVariable);    
    free(lluvia);
}

Upvotes: 0

Views: 522

Answers (1)

Barmar
Barmar

Reputation: 781716

You can only call free(ptr) if the value of ptr was returned from malloc() or realloc(). Your code later has:

fecha = strtok(fecha, "/");

So when you get to the end of the function, fecha no longer contains the pointer that it originally contained when you did:

fecha = malloc(1000 * sizeof(char));

You should use a different variable in the loop so that you don't lose the original fecha pointer.

You have the same problem with:

fechaVariable = strtok(fechaVariable,"/");

so use a different variable here as well.

Actually, there's no good reason to use dynamic allocation for these variables at all. Just declare:

char fecha[1000], fechaVariable[1000], lluvia[1000];

Then use different variables with strtok, since you can't assign to array variables.

char *fecha_slash = strtok(fecha, "/");
char *fechaVariable_slash = strtok(fechaVariable, "/");

Upvotes: 1

Related Questions