Ç可变参数 - va_copy问题 [英] C varargs - va_copy issues

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

问题描述

我在C语言编写一个函数,它采用可变数量的参数。

I'm writing a function in C that takes a variable number of arguments.

size_t myprintf(char *fmt, ...);

到目前为止,一切都很好。我已经决定这是最好的做事情的正确方法™,使一个版本,采用可变参数,另一个版本,需要一个的va_list

size_t myprintf(char *fmt, ...);
size_t myvprintf(char *fmt, va_list args);

并不难做到。除了 my_vprintf()需要发送的 ARGS 到两个不同的功能(第一个的snprintf (),长度为0,以确定我们的房间需要多少,然后的sprintf()之后,我们已经分配了很大的空间)。我这样做与 va_copy

Not that hard to do. Except my_vprintf() needs to send its args out to two different functions (first to snprintf() with a length of 0 to determine how much room we need, then to sprintf() after we've allocated that much room). I do this with va_copy.

size_t myvprintf(char *fmt, va_list args)
{
    va_list args2;
    va_copy(args, args2);
    // do stuff with args2
    va_end(args2);
    // do more stuff with args
}

这是所有罚款和花花公子,但C99是有点执行不力。我想,如果可能的话,我的code在C89以及工作,并与尽可能多的编译器和尽可能多的平台,尽可能的工作。我现在有这后的#include<&STDDEF.H GT; 但在此之前的任何code:

This is all fine and dandy, but C99 is a bit poorly implemented. I would like, if possible, for my code to work in C89 as well, and to work with as many compilers and on as many platforms as possible. I currently have this after #include <stddef.h> but before any code:

#ifndef va_copy
# ifdef __va_copy
#  define va_copy(a,b) __va_copy(a,b)
# else /* !__va_copy */
#  define va_copy(a,b) ((a)=(b))
# endif /* __va_copy */
#endif /* va_copy */

我认为,导致((A)=(B))是不可靠的,而我也许应该使用的memcpy()或类似的东西,但是这仍然是对的:如果你不支持C99,我希望工程,而不是如果你不支持C99,从不畏惧(这是什么水平我想要)。有没有什么好的办法来解决这个限制?我见过几个解决方案 - 即吃一个参数和递归的va_list 函数,传递的va_list 两次这样两个单独的副本是由等 - 但我不知道他们会如何工作(和递归解决方案不会做的这么好,如果我只是想打电话给 vsnprintf(),现在会吗?)。

I am led to believe that ((a)=(b)) is unreliable, and that I should perhaps use memcpy() or something similar, but this is still on the level of "If you don't support C99, I hope it works" rather than "If you don't support C99, never fear" (which is what I want). Is there any good way to get around this limitation? I've seen a few solutions - va_list functions that eat one argument and recurse, passing the va_list twice so that two separate copies are made, etc. - but I don't know how well they would work (and the recursive solution won't do so well if I just want to call vsnprintf(), now, will it?).

所以我转交给你,StackOverflow的用户。有什么更多的,我可以做些什么来提供C89的兼容性,或者是没有 va_copy 用户和 __ va_copy (当然少之又少之间),只是将不得不给它吸走了吗?

So I turn to you, StackOverflow User. Is there anything more I can do to provide C89 compatibility, or are users without va_copy and __va_copy (admittedly few and far between) just going to have to suck it in and take it?

推荐答案

(A)=(B)是靠不住的。即使传递两个的va_list 是(公共函数是一个简单的包装)为va_list的可能是这样的:

(a)=(b) is unreliable. Even passing two va_list is (the public function would be a simple wrapper) as va_list can be something like:

typedef __va_list_impl va_list[1];

有关这从一个做的va_​​arg将修改其他(我似乎记得使用的Solaris东西这类啊,寄存器窗口...)。可悲的是,我知道没有法子,做你想做的。

for which doing a va_arg on one will be modify the other (I seem to remember that Solaris use such kind of thing, ah, the register windows...). Sadly, I know of no sure way to do what you want.

这篇关于Ç可变参数 - va_copy问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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