If an array of function pointers is statically allocated, do I still need to free()
it?
Lets say I have this code:
typedef void (*test_ptr)(void);
int main(void)
{
test_ptr test[3];
test[0] = a;
test[1] = b;
test[2] = c;
}
So, when I would be done with it, would I have to free all the pointers, maybe like so:
for(int i = 0; i < 3; i++) {
free(test[i]);
}
Or is it automatically de-allocated when function ends, like other arrays?
So, when I would be done with it, would I have to free all the pointers
No. You must not because free(ptr)
is used only when pointer ptr
is previously returned by any of malloc
family functions.
Passing free
a pointer to any other object (like a variable or array element) causes undefined behaviour.
[...] If
ptr
is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call tofree
orrealloc
, the behavior is undefined.
Or is it automatically de-allocated when function ends, like other arrays?
Yes. It will no longer exist once its enclosing function returns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With