What is guaranteed about the size of a function pointer?

前端 未结 4 530
梦谈多话
梦谈多话 2020-11-27 06:15

In C, I need to know the size of a struct, which has function pointers in it. Can I be guaranteed that on all platforms and architectures:

  • the size of a void*
4条回答
  •  庸人自扰
    2020-11-27 06:56

    In addition to the other answers, Wikipedia says this:

    http://en.wikipedia.org/wiki/Function_pointer

    Although function pointers in C and C++ can be implemented as simple addresses, so that typically sizeof(Fx)==sizeof(void *), member pointers in C++ are often implemented as "fat pointers", typically two or three times the size of a simple function pointer, in order to deal with virtual inheritance.

    A function pointer is an abstraction. As long as the requirements of the standard are fulfilled, anything is possible. I.e. if you have less than 256 functions in your program, function pointers could be implemented by using a single byte with the value 0 for NULL and the values 1 to 255 as the index into a table with the physical addresses. If you exceed 255 functions, it could be extended to use 2 bytes.

提交回复
热议问题