如何可变参数的函数找出机器code参数的数目? [英] How do vararg functions find out the number of arguments in machine code?

查看:109
本文介绍了如何可变参数的函数找出机器code参数的数目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能像可变参数的函数的的printf 的找出他们得到的参数是多少?

How can variadic functions like printf find out the number of arguments they got?

的论点显然是不作为(隐藏)参数传递的量(见的调用这里ASM例如给printf )。

The amount of arguments obviously isn't passed as a (hidden) parameter (see a call to printf in asm example here).

有什么诀窍?

推荐答案

的技巧是,你告诉他们莫名其妙地别的。对于的printf 你必须提供一个格式字符串其中甚至包含类型信息(这可能是不正确的,虽然)。提供此信息的方式主要是用户合同,往往容易出错。

The trick is that you tell them somehow else. For printf you have to supply a format string which even contains type information (which might be incorrect though). The way to supply this information is mainly user-contract and often error-prone.

对于调用约定:通常情况下,参数被压入堆栈由左到右,然后backjump地址最后。调用例程清除堆栈。所以没有技术需要调用的例程知道的参数的数目。

As for calling conventions: Usually the arguments are pushed onto the stack from left to right and then the backjump address at last. The calling routine clears the stack. So there is no technical need for the called routine to know the number of parameters.

编辑:在C ++ 0x中有一个安全的方式(甚至是类型安全的!)来调用可变参数的函数

In C++0x there is a safe way (even typesafe!) to call variadic functions!

这篇关于如何可变参数的函数找出机器code参数的数目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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