This is a simple program that reads a file "hello.txt" into a dynamically allocated buffer, initially of size 10 (doubled in size whenever it is filled up)
When running valgrind, there appears to be a memory leak, but I'm not sure what the issue is. I free'd the buffer's memory after use.
The error appears to be "Conditional jump or move depends on uninitialised value(s)"
Can anyone help identify if there is a memory leak? And if not what is the issue?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFERSIZE 10
int main(int argc, char const *argv[])
{
FILE *source;
source = fopen("hello.txt","r");
char *buffer = (char *)malloc(BUFFERSIZE);
int current_size = BUFFERSIZE;
int len = 0;
char c;
while((c = fgetc(source)) != EOF)
{
if(len == current_size-1)
{
current_size *= 2;
buffer = (char *)realloc(buffer,current_size);
}
buffer[len] = c;
len++;
}
printf("%s",buffer);
free(buffer);
return 0;
}