James51332
James51332

Reputation: 173

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?

Upvotes: 2

Views: 212

Answers (3)

dbush
dbush

Reputation: 224437

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.

Upvotes: 9

John Bode
John Bode

Reputation: 123558

Pointers to different types may have different sizes, and it is possible for a pointer to a function taking no arguments to have a different size and/or alignment from a pointer to a function taking some number of arguments (I can't imagine why they would, but it's possible). It's also possible for them to have the same size and alignment.

Whether they do or not depends entirely on the specific implementation. On systems with a "flat" memory model (modern Windows, MacOS, and Linux on x86 and x86-64, for example), all object pointer and function pointer types tend to be the same size. On segmented systems like DOS you had near (16-bit) and far (32-bit) pointers depending on whether you were referencing something in the same memory segment or not. Harvard architectures keep code and data in physically separate memory with different address bus sizes, so pointers to functions may have different sizes than pointers to objects.

In C++ the only requirements (AFAICT, anyway) are that pointers to qualified types have the same size and alignment as pointers to their unqualified equivalent, and that void * and char * have the same size and alignment.

You should never be surprised if two different pointer types have the same size. You should never be surprised if two different pointer types have different sizes (modulo the requirements above).

Upvotes: 1

dev65
dev65

Reputation: 1605

this is because it is just a pointer like any pointer but points to function with a specific signature so that the compiler can deduce how to make a call in the call site . A pointer is typically a number containing the address of the object it points to , an it (the pointer) has the size of the cpu arch the program is compiled for . so x64 pointers are 8 bytes = 64 bits, x32 pointers are 4 bytes = 32 bits and so on

Upvotes: 0

Related Questions