最后命名参数无法正常运行或数组? [英] Last named parameter not function or array?

查看:164
本文介绍了最后命名参数无法正常运行或数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是关于可变参数的函数,并把它们的最后一个命名参数,省略号前:

This question is about vararg functions, and the last named parameter of them, before the ellipsis:

void f(Type paramN, ...) {
  va_list ap;
  va_start(ap, paramN);
  va_end(ap);
}

我读的C标准,发现了的va_start 宏以下限制:

参数parmN是在功能去连接nition变量参数列表最右边的参数(一个之前只是...)的鉴定连接器。如果参数parmN与寄存器存储类,与函数或数组类型声明,或者用一个类型,是不是与默认参数提升的应用之后,所获得的类型兼容,该行为是理解过程网络定义。

The parameter parmN is the identifier of the rightmost parameter in the variable parameter list in the function definition (the one just before the , ...). If the parameter parmN is declared with the register storage class, with a function or array type, or with a type that is not compatible with the type that results after application of the default argument promotions, the behavior is undefined.

我不知道为什么这种行为是未定义以下code

I wonder why the behavior is undefined for the following code

void f(int paramN[], ...) {
  va_list ap;
  va_start(ap, paramN);
  va_end(ap);
}

而不是未定义以下

and not undefined for the following

void f(int *paramN, ...) {
  va_list ap;
  va_start(ap, paramN);
  va_end(ap);
}

宏旨在通过纯C code实现的。但纯C code不能找出与否 paramN 被宣布为一个数组或一个指针。在这两种情况下,该参数的类型被调整为一个指针。这同样适用于函数型参数真。

The macros are intended to be implementable by pure C code. But pure C code cannot find out whether or not paramN was declared as an array or as a pointer. In both cases, the type of the parameter is adjusted to be a pointer. The same is true for function type parameters.

我在想:什么是这个限制的原因是什么?做一些编译器有实现此问题时,这些参数调整到位内部? (同样不确定的行为中指出了C ++ - 所以我的问题是关于C ++藏汉)。

I wonder: What is the rationale of this restriction? Do some compilers have problems with implementing this when these parameter adjustments are in place internally? (The same undefined behavior is stated for C++ - so my question is about C++ aswell).

推荐答案

针对寄存器参数或功能参数的限制很可能是这样的:

The restriction against register parameters or function parameters are probably something like:


  • 您不允许随身携带一个变量的地址注册存储类。

  • 函数指针有时比对象指针完全不同。例如,他们可能比对象指针较大(你不能一个函数指针可靠地转换为对象的指针,然后再返回),因此增加了一些固定数量的一个函数指针的地址可能不会让你到下一个参数。如果的va_start()和/或在va_arg()中加入一些固定的金额<$ C的地址被执行比对象指针计算最终会与错误的地址对象在va_arg()返回$ C> paramN 和函数指针较大。这可能似乎并不为实现这些宏的好方法,但也有可能是有(甚至需要),这种类型的实现平台。

  • you are not allowed to take the address of a variable with the register storage class.
  • function pointers are sometimes quite different than pointers to objects. For example, they might be larger than pointers to objects (you can't reliably convert a function pointer to an object pointer and back again), so adding some fixed number to the address of a function pointer might not get you to the next parameter. If va_start() and/or va_arg() were implemented by adding some fixed amount to the address of paramN and function pointers were larger than object pointers the calculation would end up with the wrong address for the object va_arg() returns. This might not seem to be a great way to implement these macros, but there might be platforms that have (or even need) this type of implementation.

我想不出的问题将是prevent允许数组参数的话,但PJ Plauger说,这在他的著作标准C库:

I can't think of what the problem would be to prevent allowing array parameters, but PJ Plauger says this in his book "The Standard C Library":

一些强加在中所定义的宏的限制; STDARG.H&GT; 似乎不必要的严重。对于一些实施方式,它们是。每个被引入,但是,以满足至少一个严重C实现的需要。

Some of the restrictions imposed on the macros defined in <stdarg.h> seem unnecessarily severe. For some implementations, they are. Each was introduced, however, to meet the needs of at least one serious C implementation.

和我想象还有谁更了解C库比Plauger的来龙去脉几个人。我希望有人能够回答与实际的例子这一具体问题;我认为这将是琐事的一个有趣的一点。

And I imagine that there are few people who know more about the ins and outs of the C library than Plauger. I hope someone can answer this specific question with an actual example; I think it would be an interesting bit of trivia.

新信息:


在理由国际标准 - 编程语言 - C表示,这约的va_start()

The "Rationale for International Standard - Programming Languages - C" says this about va_start():

parmN 参数的va_start 的用意是为实现者编写的辅助
  完全用C一个符合的va_start 宏定义,即使使用pre-C89编译器(例如,通过取参数的地址)。在 parmN 参数的声明的限制,从意图遵循允许这种实现的,因为应用和放大器;操作员参数名称可能不会产生预期的结果,如果该参数的声明不符合这些限制。

The parmN argument to va_start was intended to be an aid to implementors writing the definition of a conforming va_start macro entirely in C, even using pre-C89 compilers (for example, by taking the address of the parameter). The restrictions on the declaration of the parmN parameter follow from the intent to allow this kind of implementation, as applying the & operator to a parameter name might not produce the intended result if the parameter’s declaration did not meet these restrictions.

这并不是说,可以帮助我与数组参数的限制。

Not that that helps me with the restriction on array parameters.

这篇关于最后命名参数无法正常运行或数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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