I'm doing some test and I'm a bit confused about what valgrind reports, it would be great if someone could help me understand the output better.
Basiclly what I want to do is to create a dynamic array to hold x number of strings, for this test the strings are pre-set.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *array[4]={"str1", "str4", "str0", "str2"};
int arr_index = 0;
char* get_array(void)
{
if (arr_index >= (sizeof(array)/sizeof(array[0])))
{
return NULL;
}
else
{
return array[arr_index];
}
}
int main(void)
{
char **orderIds = malloc(0);
char *tmp=NULL;
int variableNumberOfElements=0;
for(int i = 0; i < 10; i++) {
tmp = get_array();
if(tmp == NULL)
{
break;
}
arr_index++;
variableNumberOfElements++;
orderIds = realloc(orderIds, variableNumberOfElements * sizeof(char*));
orderIds[i] = malloc(strlen(tmp) * sizeof(char));
strncpy(orderIds[i], tmp, strlen(tmp));
}
for(int j=0; j<variableNumberOfElements; j++)
{
printf("%s\n", orderIds[j]);
}
for(int x=0; x<=variableNumberOfElements; x++)
{
free(orderIds[x]);
}
free(orderIds);
exit(0);
}
And the output when running valgrind --leak-check=yes ./a.out
is:
==27260== Memcheck, a memory error detector
==27260== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==27260== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==27260== Command: ./a.out
==27260==
==27260== Invalid read of size 1
==27260== at 0x484ED24: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==27260== by 0x48F1EE7: puts (ioputs.c:35)
==27260== by 0x109357: main (test.c:42)
==27260== Address 0x4a9c0d4 is 0 bytes after a block of size 4 alloc'd
==27260== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==27260== by 0x1092E6: main (test.c:36)
==27260==
str1
str4
str0
str2
==27260== Invalid read of size 8
==27260== at 0x109381: main (test.c:46)
==27260== Address 0x4a9c290 is 0 bytes after a block of size 32 alloc'd
==27260== at 0x484DCD3: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==27260== by 0x1092B8: main (test.c:35)
==27260==
==27260==
==27260== HEAP SUMMARY:
==27260== in use at exit: 0 bytes in 0 blocks
==27260== total heap usage: 10 allocs, 10 frees, 1,120 bytes allocated
==27260==
==27260== All heap blocks were freed -- no leaks are possible
==27260==
==27260== For lists of detected and suppressed errors, rerun with: -s
==27260== ERROR SUMMARY: 5 errors from 2 contexts (suppressed: 0 from 0)
So basiclly it's telling me that I'm doing some invalid reads when I'm allocating memory. But I think it looks correct, what have I missed?
I have calculated the bytes allocated and I think it looks right.