I'm new to C, trying to learn dynamic memory allocation for an array of char arrays, and not sure why I can't make valgrind happy with 0 errors also whilst avoiding a segfault. My example is based on this example:
How to dynamically allocate memory for char** in C
From that example, I whipped up this test code below:
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char* argv[]){
char **myChar;
int nEl = 5;
int nChars = 10;
myChar = (char**)malloc(sizeof(char*));
for (int it = 0; it < nEl; it++) {
myChar[it] = (char*)malloc((nChars) * sizeof(char));
}
//for (int it = 0; it < nEl; it++) {
// free(myChar[it]);
//}
//free(myChar);
return 0;
}
It compiles as is, runs without problem, exits with return 0x0, but valgrind complains:
4 errors in context 1 of 1:
Invalid write of size 8
at 0x400583: main (in /home/username/Documents/personal/tmp/cprog2/test2)
Address 0x5204048 is 0 bytes after a block of size 8 alloc'd
at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x40054D: main (in /home/username/Documents/personal/tmp/cprog2/test2)
ERROR SUMMARY: 4 errors from 1 contexts (suppressed: 0 from 0)
Figuring that valgrind expects the malloc'd **myChar and myChar[it] to be free(), I uncomment the commented bits, but the program segfaults and valgrind says this:
4 errors in context 1 of 2:
Invalid read of size 8
at 0x4005EF: main (in /home/username/Documents/personal/tmp/cprog2/test2)
Address 0x5204048 is 0 bytes after a block of size 8 alloc'd
at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x40058D: main (in /home/username/Documents/personal/tmp/cprog2/test2)
4 errors in context 2 of 2:
Invalid write of size 8
at 0x4005C3: main (in /home/username/Documents/personal/tmp/cprog2/test2)
Address 0x5204048 is 0 bytes after a block of size 8 alloc'd
at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x40058D: main (in /home/username/Documents/personal/tmp/cprog2/test2)
ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 0 from 0)
Why can I not make valgrind happy and compile and run a working app?