I am trying to free () all the allocated memory of malloc (), realloc (), but valgrind says it is a memory leak.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int lines_allocated = 128;
int max_line_len = 50;
FILE *fp;
char **array = (char **)malloc(sizeof(char*)*lines_allocated);
if (array==NULL) {
fprintf(stderr,"Out of memory (1).\n");
exit(1);
}
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
fprintf(stderr,"Error opening file.\n");
exit(2);
}
int il;
for (il = 0; 1; il++) {
int j;
if (il >= lines_allocated) {
int new_size;
new_size = lines_allocated*2;
array = (char **)realloc(array,sizeof(char*)*new_size);
if (array==NULL) {
fprintf(stderr,"Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
array[il] = malloc(max_line_len);
if (array[il]==NULL) {
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(array[il], max_line_len-1, file)==NULL)
break;
for (j=strlen(array[il])-1;j>=0 && (array[il][j]=='\n' || array[il][j]=='\r');j--)
;
array[il][j+1]='\0';
}
fclose(file);
int cc;
for (cc = 0; cc < il; cc++) {
printf("%s\n", array[cc]);
free(array[cc]);
}
free(array);
return 0;
}
valgrind./main
valgrind
==4806== Memcheck, a memory error detector
==4806== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4806== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4806== Command: ./main
==4806==
1
2
3
4
5
6
7
8
9
10
11
==4806==
==4806== HEAP SUMMARY:
==4806== in use at exit: 50 bytes in 1 blocks
==4806== total heap usage: 14 allocs, 13 frees, 2,192 bytes allocated
==4806==
==4806== 50 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4806== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==4806== by 0x40092E: main (in /var/www/mem/main)
==4806==
==4806== LEAK SUMMARY:
==4806== definitely lost: 50 bytes in 1 blocks
==4806== indirectly lost: 0 bytes in 0 blocks
==4806== possibly lost: 0 bytes in 0 blocks
==4806== still reachable: 0 bytes in 0 blocks
==4806== suppressed: 0 bytes in 0 blocks
==4806==
==4806== For counts of detected and suppressed errors, rerun with: -v
==4806== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
How to free memory? It says that you need to free up another block of memory, but where is it?
source
share