If i have a pointer to an array of pointers, is the pointer to this array points to all elements in this array, i.e
if i free the pointer to the first element in that array, i should free all other pointers or not?
Thanks in advance.
If i have a pointer to an array of pointers, is the pointer to
Share
You should free exactly what you allocate (or what you acquire from functions that specifically say you own the pointer now, or that it’s “suitable for passing to
free“). Which is to say, if you allocated an array, you free the pointer to that array.Now, if the array contains pointers to other stuff you’ve allocated or acquired, then you should free those pointers as well (that is, the value of each element in the array), unless you have copies stashed elsewhere that you’re still using. But you don’t have to (and shouldn’t) free pointers to each cell in the array.