How to get the length of a function in bytes?

后端 未结 11 1097
醉酒成梦
醉酒成梦 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

    You can find the length of your C function by subtracting the addresses of functions. Let me provide you an example

    int function1()
        {
        } 
    
    int function2()
    {
        int a,b;    //just defining some variable for increasing the memory size
        printf("This function would take more memory than earlier function i.e function01 ");
    }
    
    int main()
    {
        printf("Printing the address of function01 %p\n",function01);
        printf("Printing the address of function02 %p\n",function02);
        printf("Printing the address of main %p\n",main);
        return 0;
    }
    

    Hope you would get your answer after compiling it. After compiling you will able to see the difference in size of function01 and function2.

    Note : Normally there is 16bytes diff between one function and other.

提交回复
热议问题