Is there a recommended integer type for storing function pointers in standard C

The C99 standard has uintptr_t , the recommended integer type for converting data pointers (pointers to objects) to, but I have not found an equivalent integer type for storing function pointers. Did I miss this?

A specific compiler can determine this type even if it does not conform to the standard, but the compiler is more likely to state that a function pointer can be stored in (for example) a uint64_t than to define a new type.

Another difference is that it makes sense to do integer arithmetic on a data pointer in ways that it did not point to a function pointer. One common idiom is (int*)(((uintptr_t)p + 15) & ~(uintptr_t)15) , but there is no reason to apply this idiom to a function pointer.

+4
source share
3 answers

There is no such recommended type of pointer.

To save a pointer, you can use any other type of function pointer if you do not call your function through it.

If you convert the pointer, and not back to the original type, you can name your function. It is always guaranteed.

+3
source

I did not find an equivalent integer type for storing function pointers

This is because you cannot convert pointers to integers. This leads to undefined behavior.

+3
source

There is no integer type for function pointers that need to be converted.

One reason: on some machines, function addresses can be very large, larger than any data pointers.

+3
source

Source: https://habr.com/ru/post/1498720/


All Articles