How to get the length of a function in bytes?

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

    There's no facility defined within the C language itself to return the length of a function; there are simply too many variables involved (compiler, target instruction set, object file/executable file format, optimization settings, debug settings, etc.). The very same source code may result in functions of different sizes for different systems.

    C simply doesn't provide any sort of reflection capability to support this kind of information (although individual compilers may supply extensions, such as the Codewarrior example cited by sskuce). If you need to know how many bytes your function takes up in memory, then you'll have to examine the generated object or executable file directly.

    sizeof func won't work because the expression func is being treated as a pointer to the function, so you're getting the size of a pointer value, not the function itself.

提交回复
热议问题