C 函数调用中的参数提升
我从 ----至于默认提升何时生效:当参数的预期类型未知时(也就是说,当没有原型或参数是可变参数时),使用默认参数提升。
但一个让我困惑的例子是:
void func(char a, char b)
{
printf("a=%p,b=%p\n",&a,&b);
}
int main(void)
{
char a=0x11,b=0x22;
func(a,b);
return 0;
}
上面的例子清楚了:在 main 中调用 func 时,不需要提升参数 a 和 b,但输出显示 &a = &b +4 而不是 &a =&b+1。如果没有发生提升,为什么两个 CHAR 参数之间有 4 个字节?
I learned from ----As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there's no prototype or when the argument is variadic.
But an example confusing me is:
void func(char a, char b)
{
printf("a=%p,b=%p\n",&a,&b);
}
int main(void)
{
char a=0x11,b=0x22;
func(a,b);
return 0;
}
It is cleard in the above example: when calling func in main, there is no need to promote the arguments a and b, but the output shows &a = &b +4 not &a = &b+1. If no promotion occured, why 4 bytes between two CHAR argument?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为编译器喜欢这样做:-)
您不能仅通过查看参数的地址来推断该参数是否已被提升。
不要求参数在堆栈上连续传递(甚至不要求它们根本在堆栈上传递)。
编译器(以及您平台的调用约定)可能会指定堆栈始终保持 4 字节对齐,但这是特定于实现的细节,而不是 C 语言标准的一部分。
Because the compiler feels like doing it that way :-)
You can't infer that an argument has or hasn't been promoted just by looking at its address.
There's no requirement that arguments be passed continguously on the stack (or even that they are passed on a stack at all, for that matter).
The compiler (and calling conventions for your platform) might specify that the stack is always kept 4-byte aligned, but that's an implementation-specific detail, not part of the C language standard.