I've got those two errors and could need some help to find a solution after searching for a long time:
==4902== 1 errors in context 1 of 2:
==4902== Invalid read of size 1
==4902== at 0x4010A0: getData (main.c:321)
==4902== by 0x402527: main (main.c:783)
==4902== Address 0x52007af is 1 bytes before a block of size 2,152 alloc'd
==4902== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4902== by 0x400FF1: getData (main.c:309)
==4902== by 0x402527: main (main.c:783)
==4902==
==4902==
==4902== 1 errors in context 2 of 2:
==4902== Conditional jump or move depends on uninitialised value(s)
==4902== at 0x4C2E0E9: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4902== by 0x40107A: getData (main.c:319)
==4902== by 0x402527: main (main.c:783)
==4902== Uninitialised value was created by a heap allocation
==4902== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4902== by 0x400FF1: getData (main.c:309)
==4902== by 0x402527: main (main.c:783)
char** buffer = malloc(file_size * sizeof(char**));
if(buffer == NULL)
{
status = EXITCODE_4;
return status;
}
int buffer_counter = 0;
int buffer_length = 0;
while(!feof(datafile))
{
buffer[buffer_counter] = malloc(file_size * sizeof(char*));
if(buffer[buffer_counter] == NULL)
{
status = EXITCODE_4;
free2D(buffer, buffer_counter);
return status;
}
fgets(buffer[buffer_counter], file_size, datafile);
buffer_length = strlen(buffer[buffer_counter]) - 1;
if((buffer[buffer_counter][buffer_length]) == NEWLINE)
buffer[buffer_counter][buffer_length] = 0;
buffer_counter++;
}
line 309 is where the second malloc happens, 321 the if and 319 the strlen
i'm not very experienced with valgrind, so i don't know how to fix that. Thx for any help i can get.