在 C 中,给定一个可变参数列表,如何使用它们构建函数调用? [英] In C, given a variable list of arguments, how to build a function call using them?

查看:23
本文介绍了在 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 push 等)或这个:

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 方案的外部接口:

调出是正常的函数调用.在动态环境中,我们创建一个调用接口"对象,它指定(二进制)输入/输出类型;此对象可以与任意函数指针和输入值数组,以执行对函数的调用并检索其结果.这样做需要操作堆栈并知道如何调用函数,这些是 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天全站免登陆