Thread: free(): double free detected in tcache 2 Aborted (core dumped)

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    free(): double free detected in tcache 2 Aborted (core dumped)

    I want to implement a program that reads data from a file specified as a command-line argument, having the following format:
    username, hh, mm
    where the fields are separated by a comma and might have an arbitrary number of spaces in between.
    Print the data from the file in descending order of their last login time (hh, mm), breaking ties (when the last login time is the same) by username.
    Code:
    #include <stdio.h>
    Code:
    #include <stdlib.h>
    int readFields(char* user, int* h, int* m)
    {
        user = calloc(21, sizeof(char));
        return scanf("%21s,%d,%d", user, h, m);
    //    free(user); // should I free the memory here or in main?
    }
    int main(int argc, char *argv[])
    {
        char *user = NULL;
        user = calloc(21, sizeof(char)); // do I have to dynamically allocate memory in both main and readFields?
        int hh , mm;
        if(argc != 2)
        {
            printf("Invalid format.\n");
            return -1;
        }
        FILE *file_to_read = fopen(argv[1], "r");
        if(!file_to_read)
        {
            printf("ERROR reading the file.\n");
            return 1;
        }
        if(readFields(user, &hh, &mm)==3)
        {
            printf("The first login was made by %s at %d:%d.\n", user, hh, mm);
        }
        else
            {
                printf("Invalid format.\n");
            }
        free(user);
        fclose(file_to_read);
        if(fclose(file_to_read) != 0)
        {
            printf("Error closing file.\n");
            return -1;
        }
       return 0;
    }

    I've created a .txt file that contains the following:

    andrew,22, 25
    anna, 21, 09
    michael, 23, 44
    john,12,43

    When I run the program I get no output, I should be getting
    "The first login was made by andrew at 22:25."
    Also if I write anything in the terminal after running the program, I get something strange:
    i.e.:
    ./a.out 1.txt
    abcd
    Invalid format.
    free(): double free detected in tcache 2
    Aborted (core dumped)

    I've never had this before and I don't really understand this because I didn't free the same memory block twice anywhere in the program.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,097
    Why are you bothering to dynamically allocate user? Just use an array.

    If you want to read from a file you need to pass the FILE* pointer to readFields and use fscanf.

    The "%s" format spec will read as many characters as it can until it hits whitespace, so with your first file line it will read "andrew,22,", which screws up the rest of your fields. You can use the spec "%[^,]" which reads up to, but not including, the first comma.

    Why are you trying to fclose the file twice?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int readFields(FILE *fin, char *user, int *h, int *m)
    {
        return fscanf(fin, "%[^,],%d,%d", user, h, m);
    }
     
    int main(int argc, char *argv[])
    {
        if (argc != 2)
        {
            printf("Invalid usage.\n");
            return -1;
        }
     
        FILE *fin = fopen(argv[1], "r");
        if (!fin)
        {
            printf("Cannot open the input file.\n");
            return -1;
        }
     
        char user[21];
        int hh, mm;
     
        if (readFields(fin, user, &hh, &mm) == 3)
        {
            printf("The first login was made by %s at %d:%d.\n", user, hh, mm);
        }
        else
        {
            printf("Invalid format.\n");
        }
     
        fclose(fin);
     
        return 0;
    }
    The readFields function is obviously kind of pointless here.
    Last edited by john.c; 01-20-2021 at 05:02 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    Thanks, now I'm trying to get the maximum hour from the file and if two hours are equal then check their minutes to sort them in descending order. Would it be easier to use a structure in this case?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int readFields(FILE* file_to_read, char* user, int* h, int* m)
    {
        return fscanf(file_to_read, "%20[^,],%d,%d", user, h, m); // should we give MAX length bewteen % and [^,]?
    }
    int main(int argc, char *argv[])
    {
        char user[21];
        //hp, mp = previous hour and minutes; hi, mi = current hour and minutes
        int hi, mi, hp, mp;
        if(argc != 2)
        {
            printf("Invalid format.\n");
            return -1;
        }
        FILE *file_to_read = fopen(argv[1], "r");
        if(!file_to_read)
        {
            printf("ERROR opening the file.\n");
            return 1;
        }
        if(readFields(file_to_read, user, &hp, &mp) == 3)
        {
            int maxHour = hp;
            int maxMinutes = mp;
        while(readFields(file_to_read, user, &hi, &mi) == 3)
        {
            printf("%s logged in at %d:%d.\n", user, hi, mi);
            if(hp < hi)
            {
                maxHour = hi;
                printf("%s user logged in at %d:%d\n", user, maxHour, mp);
                hp = hi;
            }
            else
            {
                maxHour = hp;
                printf("%s user logged in at %d:%d\n", user, maxHour, mi);
                hi = hp;
            }
            if(hp == hi)
            {
                if(mp > mi)
                {
                    maxMinutes = mp;
                    printf("%s logged at %d:%d\n", user, hp, maxMinutes);
                    mi = mp;
                }
                else
                {
                    maxMinutes = mi;
                    printf("%s logged at %d:%d\n", user, hi, maxMinutes);
                    mp = mi;
                }
            }
        }
        }
        /*else
            {
                printf("Invalid format.\n");
            }*/
        fclose(file_to_read);
        return 0;
    }
    .txt file:
    andrew,22, 25
    anna, 21, 09
    gogu, 22, 26
    michael, 23, 44
    john,12,43
    gigi, 05, 30

    Current output:
    anna logged in at 21:9.


    anna user logged in at 22:9


    anna logged at 22:25


    gogu logged in at 22:26.


    gogu user logged in at 22:26


    gogu logged at 22:26


    michael logged in at 23:44.


    michael user logged in at 23:26


    michael logged at 23:44


    john logged in at 12:43.


    john user logged in at 23:43


    john logged at 23:44


    gigi logged in at 5:30.


    gigi user logged in at 23:30


    gigi logged at 23:44
    Desired Output:
    michael logged at 23:44
    gogu logged at 22:26
    andrew logged at 22:25
    anna logged at 21:09
    john logged at 12:43
    gigi logged at 05:30

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to troubleshoot core dumped (aborted) message
    By zolfaghar in forum C Programming
    Replies: 1
    Last Post: 04-03-2016, 04:44 PM
  2. Replies: 2
    Last Post: 09-21-2015, 11:21 AM
  3. Pthread and malloc -Aborted (core dumped)
    By Hassan Ahmed in forum C Programming
    Replies: 11
    Last Post: 07-12-2013, 11:24 PM
  4. Aborted (core dumped)? Error when running
    By Brian Justice in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2012, 10:35 PM
  5. Replies: 1
    Last Post: 09-19-2008, 01:21 AM

Tags for this Thread