So, valgrind gave me that error and I ran it with --track-origins=yes and found the line where the error is, but I don't understand what the error is or how to fix it.
#include <stdio.h>
#include <stdlib.h>
typedef struct Date{
int year;
int month;
int day;
} Date;
typedef struct Data{
Date date;
float temp;
float uncertainty;
char country[100];
} Data;
int main(){
FILE* f = fopen("tempcountries_short.csv", "r");
char* line = NULL;
int capacity = 0;
int countries_capacity = 0;
int line_ix = 0;
char c;
Data* country = NULL;
while ((c = fgetc(f)) != '\n'){
if (line_ix + 1 > capacity){
if (capacity == 0)
capacity = 10;
else
capacity = capacity * 2;
line = realloc(line, capacity);
}
line[line_ix] = c;
line_ix++;
}
if (countries_capacity == 0)
countries_capacity = 10;
else
countries_capacity = countries_capacity * 2;
country = realloc(country, countries_capacity);
printf("%i\n",sscanf(line, "%i - %i - %i, %f , %f , %s",
&country->date.year, &country->date.month,
&country->date.day, &country->temp, &country->uncertainty,
country->country));
}
This is the output from Valgrind, with options --leak-check=full and --track-origins=yes: https://pastebin.com/EyqDGBmQ As you can see there are many other errors, and I don't understand what causes them either.
The program is reading lines from a file with data about many countries temperatures, I just took the part of the code for a single country to replicate the error, but country is supposed to be an array of many Data structs. Here's an example line from the file I'm reading:
1972-03-01,4.787,0.342,Slovakia