是函数的va_arg参数存储在堆栈或堆内存中吗? [英] Are va_arg arguments to functions stored in stack or heap memory?

查看:53
本文介绍了是函数的va_arg参数存储在堆栈或堆内存中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题为的问题.是根据变量用法将变量参数列表存储在堆栈内存中,还是将它们分配到堆中?

更重要的是存储它们的位置:为什么将它们存储在X中?

解决方案

该标准仅说明了va_arg和相关的宏"的行为,没有说明实现.它们甚至不必是宏.

根据编译器的常规"约定,可变参数可以使用堆栈或寄存器.尽管我记得过去对于某些编译器,虽然寄存器用于常规功能,但堆栈始终用于/always/用于var arg函数.gcc编译器系列似乎都同意var arg函数的调用签名与正确指定的函数完全相同,甚至K& R默认extern函数调用也遵循相同的寄存器分配模式.这意味着您的某些var args可能在寄存器中,而有些则可能在堆栈中(如果有太多的话).

va_arg为这些参数创建一个接口.再次,标准没有说如何,只是行为.可能将va_arg完全实现为上下文变量(如struct的实例),或者可能需要编译器通过操纵堆栈位置或使用堆来保留其他存储.在这种情况下,有必要调用"va_end"来释放那些堆或栈保留:这始终是最佳实践.

然后,当然,您将'va_arg'的实例传递给子v函数.现在,va_arg类型参数的作用类似于对va_arg实例的引用,尽管C通常没有引用的概念-函数声明中最接近的相似概念是数组参数,该数组参数被实现为指针.>

Question as titled. Are variable argument lists stored in stack memory based on their usage, or are they allocated into the heap?

More importantly to where they are stored: Why are they stored in X?

解决方案

The standard says nothing about the implementation, only the behaviour of va_arg and associated "macros". They don't even have to be macros.

Variable arguments my use the stack, or registers, as per the "normal" conventions for the compiler. Though I recall that for some compilers in the past, while registers were used for normal functions, stack was /always/ used for var arg functions. The gcc compiler family all seem to agree that the call signatures for var arg functions are exactly the same as properly specified functions, and even K&R default extern function calls follow the same register allocation patterns. This means that some of your var args may be in registers and some may be on the stack if there are too many.

va_arg creates an interface to those arguments. Again the standards don't say how, just the behaviour. It may be possible to implement va_arg completely as a context variable, like an instance of a struct, or it may be necessary for the compiler to reserve additional storage, either by manipulating the stack position, or by using a heap. In these cases it is necessary to call 'va_end' to release those heap or stack reservations: this is always best practice.

Then, of course, you pass your instance of 'va_arg' to a child v-function. Now, the va_arg type parameter acts like a reference to the 'va_arg' instance, though C doesn't have a concept of references generally - the nearest similar concept in function declarations is an array argument, which is implemented as a pointer.

这篇关于是函数的va_arg参数存储在堆栈或堆内存中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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