I'm making the (possibly wrong) assumption that it is implementation and/or system dependent. Is there something like INT_MAX or CHAR_BIT that would tell me the size of a pointer in memory?
A pointer points into a place in memory, so it would be 32 bits on a 32-bit system, and 64 bits in 64-bit system.
Passing a pointer to object will only increase memory consumption by the size of the pointer. Moreover it allows function to modify original object instead of a copy.
Note that all pointers are 8 bytes.
Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes.
A pointer points into a place in memory, so it would be 32 bits on a 32-bit system, and 64 bits in 64-bit system.
Pointer size is also irrelevant to the type it points at, and can be measured by sizeof(anyType*)
UPD
The way I answered this was suggested by the way the question was asked (which suggested a simple answer). Yes, I do agree that in cases like pointers to virtual method table, the size of the pointer will differ, and according to this article, it will vary on different platforms and even on different compilers within the same platform. In my case, for example (x64 ubuntu, GCC 4.6.3) it equals to 16 bytes.
Would sizeof(int*)
work? Or whatever you're meant to be checking?
It is definitely system dependant. Usually a simple data pointer can be stored in a size_t variable. In C++11 there is SIZE_MAX macro which is the maximal value for size_t. In C++11 you could also use std::intptr_t.
If you are taking member function pointers into consideration things get even more complicate. It depends among the other things if the class inherit from one or more parents, if it exposes virtual functions, and of course on the implementation.
Here you can find a detailed article about member function pointers, along with some examples for few compilers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With