What is the best way to free unsigned pointer array allocated with cmallc?
uint8_t *buf;
buf = cs_calloc(ca->len + 13);
i do like this , but i know this is not quite right , since i got compile warning :
warning: passing argument 1 of ‘free’ makes pointer from integer without a cast
for (i = 0; i < ca->len + 13; i++)
{
free(buf[i]);
buf[i] = NULL;
}
free(buf);
here is cs_calloc code :
void *cs_calloc(size_t size){
return (calloc (1,size));
}
my question are 1. the way i free , buf , i mean first free each of array's element in a loop (free (buf[i]) is correct? do i need to free the elements of array , and the free the array as above
Thanks in advance
You just need free(buf)
and nothing more - so just drop the for
and you're set. Trying to free(buf[i])
is wrong and will trick the library into treating buf[i]
(a small integer) as a pointer.
There's one and only one simple rule: only free
what you received from malloc
or calloc
.
why that compile warning happened
You got the warning because you called free
with an integer when free
expects a pointer.