char* getLine(FILE* file) {
if (file == NULL) return NULL;
size_t size = 100;
char* line = malloc(size);
if (line == NULL) return NULL;
char c;
int i = 0;
while ((c = (char) fgetc(file)) != '\n' && c != EOF) {
line[i++] = c;
if (i == size) {
char* temp = realloc(line, size *= 2);
if (temp == NULL) {
free(line);
return NULL;
}
line = temp;
}
}
if (i == 0 && c == EOF) {
free(line);
return NULL;
}
line[i] = '\0';
return line;
}
int main() {
FILE* file = fopen("substantive.txt", "r");
char* line = getLine(file);
puts(line);
fclose(file);
free(line);
return 0;
}
CLion tells me that the memory allocated of the function getLine is leaked. but why? i do free the return value?
