Got a really subtle problem. Got a class that compiled with MS VS 2013 c++ compiler, for 32 bit platform has the size of 4 bytes. The function pointer has sizeof 4 bytes. But when this class is compiled with this same compiler but included into different project to produce library, also targeted fo 32 bit platform, then the class has the *m_Function pointer occupy 16 bytes! Of course, when I'm instantiating this class from main project it thinks that class occupies 4 bytes and allocates this very memory size, while in reality it occupies 16 bytes and that causes memory overruns.
class CC1
{
public:
CC1();
void (CC1:: *m_Function) ();
};
I know that the size of pointer-to-member function can vary. But the qustion is - which compiler setting controls this? I don't care is it 4 or 16 bytes - just need them to be the same. Struct member alignment settings are the same for both projects. /vmm /vmg options? No mention of them in compiler settings in both projects.
By the way, I tried building for x64 target and in this case sizeof *m_Function is always 8 bytes, from main and libray project.
Thank you.
Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed. It is common to all data types like int *, float * etc.
A function-pointer is a 4-byte elementary item . Function-pointers have the same capabilities as procedure-pointers, but are 4 bytes in length instead of 8 bytes. Function-pointers are thus more easily interoperable with C function pointers.
Generally yes, All pointers to anything, whether they point to a int or a long or a string or an array of strings or a function, point to a single memory address, which is the same size on a machine.
Size of a pointer is fixed for a compiler. All pointer types take same number of bytes for a compiler. That is why we get 4 for both ptri and ptrc.
See here for docs page for /vm options
If you use the '/vmg' compiler option then pointer-to-member function will always be 16 bytes as you're effectively telling the compiler that it may not know the size beforehand and so has to assume the worst (virtual inheritance!).
If you use '/vmb' then the compiler must know about the inheritance pattern for the struct before use and so can use the most efficient method - in the case of simple inheritance this is 4 bytes.
Its likely that in some projects you've got '/vmg' set (which makes the class 16 bytes) and in others you dont (which makes the class 4 bytes).
/vmb is the implicit default - check your compiler command line for the libraries where this class is 16 bytes for /vmg
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