mchen.ja February 2016

Get sizeof a void (*)() function pointer in C

To aid with some precise memory allocation that I'm doing in C, I'm trying to get the sizeof a function pointer for a function with return type void that takes no parameters.

However, when I do sizeof (void (*)()), I generate a compiler warning:

function declaration isn’t a prototype [-Wstrict-prototypes]

How do I get the size that I'm looking for?

Answers


2501 February 2016

That is an old style function definition, it is missing the argument list, so add them:

sizeof( void(*)(void) )


Basile Starynkevitch February 2016

For readability purposes, I recommend declaring a type for the signature of any function you'll want have some pointer to:

 typedef void my_sigt(void);

then, to declare a pointer to such a function, code:

 my_sigt* funptr;

and to get its size code sizeof(my_sigt*) (or sizeof(funptr) if you have such a variable funptr)

BTW, I am not sure that the C99 standard guarantees that every function pointer has the same size (or has the same size as some data pointer). But POSIX requires that (in particular, to be able to use dlsym for dynamic linking of such functions).

Post Status

Asked in February 2016
Viewed 2,333 times
Voted 5
Answered 2 times

Search




Leave an answer