So I don\'t really know how to put the title this time. First of all I\'d like to say that I\'ve seen several comments on this page about warning if the question is related
array = realloc (array, 0);
Realloc with a size of zero is equivalent to free() on some C implementations, but not all.
And the problem is that free doesn't work, it does not free the space used
Think about what char **array
means and how it is allocated in your application. Often pointers-to-pointers are used as two-dimensional arrays, expressed as an array of arrays. Most applications allocate these with multiple calls to malloc(). array
is simply an array of char *
, where each element of that array is an array of char
. Simply calling free() on the array of char *
will free the array of char *
, but not each of the arrays of char
.
You need to call free() multiple times as described here.
I tried with different numbers and it works everytime, but if I don't do that +1 it does not work correctly.
C strings are null terminated, which means that the program keeps track of where the string ends by putting a nul character at the end of the string. That means that a C string of length N needs space for N characters, plus one nul character. The overall length of the memory space is then N+1.