Reputation: 25
I'm writing code to free a generic double pointer in C (void **ptr
). How do I check to see if the pointer was allocated via malloc
/calloc
? How do I free the internal pointers? (I learned to do this by using a for
loop and being given the number of members in the pointer, but I am not given that here.). The function declaration is
void freeArray(void **ptr);
How would you go about freeing this pointer, and error checking to make sure you won't get a seg fault or memory leak or other memory error?
Upvotes: 1
Views: 921
Reputation: 68
freeing pointer does not change wether it's allocated using malloc or calloc, if you wanna free array of pointers you should know before length of it. unless you now that the last element of the array is NULL so you can loop and stop when encounter NULL.
Upvotes: 0
Reputation: 144820
There is no portable way to determine if ptr
points to an allocated block or not, nor how large this block is, nor whether the pointers in this block are valid and point to allocated memory.
freeArray
is given a pointer to an array of void
pointers. Since there is no information about the number of pointers to free, an assumption must be made:
either the number of pointers is fixed. Say your program is dealing with arrays of 3 pointers everywhere, freeArray
might be assumed to free 3 pointers...
or the array could be null terminated, such as the argv
array passed to main
. Note however that this particular array of strings is not meant to be freed this way.
Look at the code that allocates the arrays and determine which convention is used.
Here is a naive implementation of freeArray()
for a null terminated allocated array of pointers to allocated blocks:
void freeArray(void **ptr) {
if (ptr) {
for (size_t i = 0; ptr[i]; i++) {
free(ptr[i]);
}
free(ptr);
}
}
Upvotes: -1
Reputation: 1765
I'm writing code to free a generic double pointer in C (void **ptr).
I believe there is not such thing as a generic double pointer. A pointer is just that: a pointer.
How do I check to see if the pointer was allocated via malloc/calloc?
You do not. A pointer is not allocated. Memory is allocated and the address of the area is assigned to a pointer.
How do I free the internal pointers? (I learned to do this by using a for loop and being given the number of members in the pointer, but I am not given that here.). The function declaration is
void freeArray(void **ptr);
"Internal pointer" is not a great description for this use, I believe.
Anyway, compare your function prototype with the usual main() prototype
int main(int argc, char** argv)
and you will see something is missing here.
Maybe it is more clear if you write
void** ptr = NULL;
You are declaring ptr
. As a pointer. But ptr
is allocated by now. It is static. May be 4 may be 8 bytes. And it points to a pointer to void
. Just this.
ptr
is void**
, *ptr
is void*
, **ptr
is void
. When using ptr
as a block of pointers you must do the same as the system does with main()
: build the block and keep your own argc
, the count of values. It is your problem to do that, or better, it is you problem NOT to do that.
Try this:
void freeArray(int ptrc, void** ptr);
And keep this pair always together and updated, so the part of "being given the number of members in the pointer" is a no brainer.
Upvotes: 1