Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of pointer to member function varies like crazy

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.

like image 234
southerton Avatar asked Apr 13 '15 14:04

southerton


People also ask

What does pointer size depend on?

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.

What is the size of function pointer?

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.

Are all pointers the same size?

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.

Why the size of pointer is 4 bytes?

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.


1 Answers

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

like image 149
Mike Vine Avatar answered Sep 20 '22 23:09

Mike Vine