Search code examples
c++memoryfunction-pointerssizeof

Why don't function pointer arguments affect memory size?


Recently I was looking at the src code for the gl3w library. I noticed that it used a new type of structure for me: a union. I found out that a union was a way for multiple data types to exist in the same place. But what I noticed was that it was switching between the type without reassigning values. It was switching from an array of pure c function pointers and a struct of function pointers with different amounts of arguments. So I did a little bit of testing:

#include <iostream>

typedef void (*PUREPROC)();
typedef void (*ARGSPROC)(int, int *);

int main() {
     std::cout << "PUREPROC's size is: " << sizeof(PUREPROC) << std::endl;
     std::cout << "ARGSPROC's size is: " << sizeof(ARGSPROC) << std::endl;

     return 0;
}

To my surprise, Both used 8 bytes of memory. I understood how gl3w was able to switch between the array and the struct, they used the same amount of memory. The array was simply a tool allowing the functions to be assigned to in an easier fashion. I knew the code worked and understood that they didn't consume different amounts of memory, but I didn't know why. So that's my question: Why don't function pointer arguments affect memory size?


Solution

  • A function pointer is still just a pointer, i.e. it contains a memory address.

    It doesn't contain anything about what the pointer points to. That's part of the language, not the representation of a pointer.