How to get the length of a function in bytes?

后端 未结 11 1112
醉酒成梦
醉酒成梦 2020-11-28 11:16

I want to know the length of C function (written by me) at runtime. Any method to get it? It seems sizeof doesn\'t work here.

11条回答
  •  情话喂你
    2020-11-28 11:45

    Just subtract the address of your function from the address of the next function. But note it may not work on your system, so use it only if you are 100% sure:

    #include 
    
    int function() {
        return 0;
    }
    
    int function_end() {
        return 0;
    }
    
    int main(void) {
        intptr_t size = (intptr_t) function_end - (intptr_t) function;
    }
    

提交回复
热议问题