I am getting this error:
Error in `./sorter': double free or corruption (!prev): 0x0000000000685010
and then a bunch of numbers which is the memory map. My program reads a CSV file of movies and their attributes from stdin and tokenizes it. The titles of the movies with commas in them are surrounded in quotes, so I split each line into 3 tokens and tokenize the front and back token again using the comma as the delimeter. I free all my mallocs at the end of the code but I still get this error. The csv is scanned until the end but I get an the error message. If I don't free the mallocs at all I don't get an error message but I highly doubt it is right. This is my main() :
char* CSV = (char*)malloc(sizeof(char)*500);
char* fronttoken = (char*)malloc(sizeof(char)*500);
char* token = (char*)malloc(sizeof(char)*500);
char* backtoken = (char*)malloc(sizeof(char)*500);
char* title = (char*)malloc(sizeof(char)*100);
while(fgets(CSV, sizeof(CSV)*500,stdin))
{
fronttoken = strtok(CSV, "\""); //store token until first quote, if no quote, store whole line
title = strtok(NULL,"\""); //store token after first quote until 2nd quote
if(title != NULL) //if quotes in line, consume comma meant to delim title
{
token = strtok(NULL, ","); //eat comma
}
backtoken = strtok(NULL,"\n"); //tokenize from second quote to \n character (remainder of line)
printf("Front : %s\nTitle: %s\nBack: %s\n", fronttoken, title, backtoken); //temp print statement to see front,back,title components
token = strtok(fronttoken, ","); //tokenizing front using comma delim
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, ",");
}
if (title != NULL) //print if there is a title with comma
{
printf("%s\n",title);
}
token = strtok(backtoken,","); //tokenizing back using comma delim
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, ",");
}
}
free(CSV);
free(token);
free(fronttoken);
free(backtoken);
free(title);
return 0;