获取C ++函数的大小 [英] Getting The Size of a C++ Function

查看:591
本文介绍了获取C ++函数的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读这个问题,因为我在一个C ++程序中试图找到一个函数的大小,它暗示可能有一种平台特定的方式。我的目标平台是windows

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 targeted platform is windows

我现在的方法如下:

1.获取一个指向函数的指针

2.增加指针(&计数器),直到我达到 ret

的机器代码值3.计数器将是函数的大小?

The method I currently have in my head is the following:
1. Obtain a pointer to the function
2. Increment the Pointer (& counter) until I reach the machine code value for ret
3. The counter will be the size of the function?

Edit1:为了澄清'size'的含义,我的意思是组成函数的字节数(机器码)。< br>
Edit2:有几个意见询问我为什么或我打算怎么做。诚实的答案是我没有意图,我不能真正看到知道一个函数长度的预编译时间的好处。 (虽然我敢肯定有一些)

To clarify what I mean by 'size' I mean the number of bytes (machine code) that make up the function.
There have been a few comments asking why or what do I plan to do with this. The honest answer is I have no intention, and I can't really see the benefits of knowing a functions length pre-compile time. (although I'm sure there are some)

这似乎是一个有效的方法给我,这将工作吗?

This seems like a valid method to me, will this work?

推荐答案

不,这不会工作:


  1. 包含单个 ret 指令。

  2. 即使它只包含一个 ret ,您不能只看单个字节 - 因为相应的值可能只是一个值,而不是一条指令。

  1. There is no guarantee that your function only contains a single ret instruction.
  2. Even if it only does contain a single ret, you can't just look at the individual bytes - because the corresponding value could appear as simply a value, rather than an instruction.

如果你限制你的编码风格,例如在你的函数中只有一个返回点,第一个问题可能可以解决,但是另一个基本上需要一个反汇编器,所以你可以分开单独的指令。

The first problem can possibly be worked around if you restrict your coding style to, say, only have a single point of return in your function, but the other basically requires a disassembler so you can tell the individual instructions apart.

这篇关于获取C ++函数的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆