Getting The Size of a C++ Function

后端 未结 16 2168
醉酒成梦
醉酒成梦 2020-12-06 09:37

I was reading this question because I\'m trying to find the size of a function in a C++ program, It is hinted at that there may be a way that is platform specific. My target

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 09:58

    Wow, I use function size counting all the time and it has lots and lots of uses. Is it reliable? No way. Is it standard c++? No way. But that's why you need to check it in the disassembler to make sure it worked, every time that you release a new version. Compiler flags can mess up the ordering.

    static void funcIwantToCount()
    {
       // do stuff
    }
    static void funcToDelimitMyOtherFunc()
    {
       __asm _emit 0xCC
       __asm _emit 0xCC
       __asm _emit 0xCC
       __asm _emit 0xCC
    }
    
    int getlength( void *funcaddress )
    {
       int length = 0;
       for(length = 0; *((UINT32 *)(&((unsigned char *)funcaddress)[length])) != 0xCCCCCCCC; ++length);
       return length;
    }
    

    It seems to work better with static functions. Global optimizations can kill it.

    P.S. I hate people, asking why you want to do this and it's impossible, etc. Stop asking these questions, please. Makes you sound stupid. Programmers are often asked to do non-standard things, because new products almost always push the limits of what's availble. If they don't, your product is probably a rehash of what's already been done. Boring!!!

提交回复
热议问题