Free array of function pointers

user1806687

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?

haccks

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.

C11: 7.22.3.3 The free function:

[...] 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 to free or realloc, 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Free array of function pointers

From Dev

Function to free pointers not working

From Dev

How to free pointers in array of structs

From Dev

Passing "array of pointers" to function

From Dev

C array of function pointers

From Dev

Passing An Array of Pointers to Function

From Dev

Accessing array of function pointers

From Dev

Array of Function Pointers in JavaScript?

From Dev

C -- Array of Function Pointers

From Dev

Malloc an array of function pointers

From Dev

Passing array of pointers into a function

From Dev

C -- Array of Function Pointers

From Dev

Filling an array of pointers in a function

From Dev

Passing An Array of Pointers to Function

From Dev

Passing array of pointers into a function

From Dev

Array of Array of function pointers in C

From Dev

How can I free an array of pointers where each pointer points to an address of a string which I allocated inside of a function?

From Dev

Coding printf with array of function pointers

From Dev

Modifiying array pointers inside a function

From Dev

How to pass array of function pointers to another function

From Dev

How to call a function that is in an array of function pointers?

From Dev

C - Does freeing an array of pointers also free what they're pointing to?

From Dev

Pass array of function pointers via SWIG

From Dev

Storing SYSCALL functions in array of function pointers

From Dev

Unable to "point to" passed array of function pointers in C

From Dev

C - How to setup an associative array of function pointers?

From Dev

C - how to modify an array of pointers inside of a function

From Dev

How do I return an array of pointers in a function?

From Dev

C++ Declaring An Array Of Function Pointers

Related Related

HotTag

Archive