填充的va_list [英] Populating a va_list

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

问题描述

有没有办法从头开始创建一个的va_list ?我试图调用一个函数,它接受一个的va_list 作为参数:

Is there a way to create a va_list from scratch? I'm trying to call a function that takes a va_list as a parameter:

func(void **entry, int num_args, va_list args, char *key);

...从一个不带有可变数量的参数的函数。我能想到的唯一方法是创建一个中介函数采用可变参数,然后沿其va_list的传球,这是pretty愚蠢的:

...from a function that doesn't take a variable number of arguments. The only way I can think of is to create an intermediary function that takes varargs and then passing along its va_list, which is pretty stupid:

void stupid_func(void **entry, char *key, int num_args, ...) {
    va_list args;
    va_start(args, num_args);

    func(entry, num_args, args, key);

    va_end(args);
}

有没有更好的办法?我不能改变 FUNC 的签名。

推荐答案

这是一个坏主意因为va_list的抽象是有隐藏某些严峻编译器/特定的体系结构有关堆栈指针并没有什么细节。而且它是非常必然一旦被初始化函数的范围pretty。如果风堆栈和引用previous帧va_args超出范围,事情可能变坏。您可以通过他们周围,但是...

This is a bad idea because the va_list abstraction is there to hide some grim compiler/architecture specific details regarding stack-pointers and what not. And it is pretty much bound to the function's scope once initialized. If you wind the stack and reference a previous frames va_args out of scope, things can go bad. You can pass them around but ...

期望的错误

请参阅:<一href=\"http://lists.freebsd.org/pipermail/freebsd-amd64/2004-August/001946.html\">http://lists.freebsd.org/pipermail/freebsd-amd64/2004-August/001946.html

另外结账的人(3)va_copy和朋友的安全的va_args处理和周围路过他们。

Also checkout man(3) va_copy and friends for safer handling of va_args and passing them around.

恕我直言va_args的东西不是很整齐。在过去的我已经然后使用指针运算的工作数据初始化堆结构/不透明指针处理这个。但是,这是一个黑客,并视情况而定。

IMHO the va_args stuff is not very neat. In the past I have dealt with this by initializing structures/opaque pointers on the heap then using pointer arithmetic to work the data. But this is a hack and depends on circumstances.

这篇关于填充的va_list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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