在C中,给出的参数变量列表,如何建立使用这些函数调用? [英] In C, given a variable list of arguments, how to build a function call using them?

查看:176
本文介绍了在C中,给出的参数变量列表,如何建立使用这些函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有莫名其妙存储参数列表,例如数组

Suppose there's a list of arguments stored somehow, in a array for example.

给定一个函数指针,我怎么能让它调用传递参数存储的名单?

Given a function pointer, how could I make a call to it passing the stored list of arguments?

我并不想传递数组作为确定参数。你猜对了,好吗?我想它的每个元素作为参数传递。数组只是为了说明问题,我可以存储一些元组结构的参数。此外,该看的我手头上有一个函数指针,并可能在字符串格式的签名即可。我并不想只是定义一个函数,能够应对可变参数列表。

I'm not trying to pass the array as an argument ok. You got it, ok? I want to pass each of its elements as an argument. An array is just to illustrate, I could be storing the arguments in some tuple structure. Also, look that I have at hand a function pointer and may have a signature in string format. I'm not trying to just define a function that is able to deal with a variadic list.

我看到如何做到这一点的唯一方法是通过采用组件(由 __ ASM推等)或本:

The only way I see how to do that is by employing assembly (by __asm push et al.) or this:

void (*f)(...);

int main()
{
    f = <some function pointer>;
    int args[]; <stored in a array, just to illustrate>
    int num_args = <some value>;

    switch(num_args)
    {
        case 0:
            f();
        break;

        case 1:
            f(args[0]);
        break;

        case 2:
            f(args[0], args[1]);
        break;

        /* etc */
    }

    return 0;
}

我不喜欢这种方法太多了......

I don't like this approach too much...

有另一种便携式和较短的形式?

Is there another portable and shorter form?

一些脚本语言都能够调用C函数。

Several script languages are able to call C functions.

脚本语言如Python或Ruby怎么做到的?他们是如何实现它在便携方式吗?难道他们只是使用汇编几个平台或以上的到底是谁?

How script languages like Python or Ruby do that? How they implement it in a portable way? Does they just use assembly for several platforms or the above in the end?

你看,我真的不问从脚本语言到C的参数安排和其他的东西的细节,我感兴趣的只是如何,到了最后,在内部,通过脚本语言调用C函数是建造的。

Look that I'm really not asking about details of parameter marshaling and other stuff from script languages to C, I'm interested only in how, in the end, internally, the call to the C function by the script language is built.

我会保持这个问题的标题,但我认为它要求一个更好的方法是:

I'll keep the question's title but I think a better way for asking it is:

如何只在运行?调用与它的指针和可用签名的C函数

外部接口为PLT的计划:

一个呼出是一个正常的函数调用。在动态环境中,
  我们创建了一个呼叫界面对象,特定网络ES(二进制)
  输入/输出类型;该对象可以以任意使用
  函数指针和输入值的数组进行标注的功能和检索其结果。这样做需要
  操纵堆栈和了解函数是如何被调用,
  这些都是细节 libffi 与交易。

A call-out is a normal function call. In a dynamic setting, we create a "call-interface" object which specifies (binary) input/output types; this object can be used with an arbitrary function pointer and an array of input values to perform a callout to the function and retrieve its result. Doing this requires manipulating the stack and knowing how a function is called, these are details that libffi deals with.

感谢@AnttiHaapala搜索,发现和指出 libffi 。这就是我一直在寻找,它正在被一群脚本语言,它是一个可移植的库,在多个架构和编译器实现的。

Thanks @AnttiHaapala for searching, finding and pointing libffi. It's what I was looking for, it's being used by a bunch of script languages, it's a portable library, implemented across several architectures and compilers.

推荐答案

我libffi的作者。它会做你所要求的。

I am the author of libffi. It will do what you are asking.

这篇关于在C中,给出的参数变量列表,如何建立使用这些函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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