[英]Contiguous memory guarantees with C++ function parameters
Appel [App02]非常简要地提到C(
is the apparent mismatch between the need for parameters being in memory due the use of address-of as opposed to in registers due to calling conventions, maybe that is a question for another day. 是由于使用地址而导致参数在内存中的需求明显不匹配时, 的。在寄存器中由于调用约定,这可能是另一天的问题。
互联网上有人是错的,有时候有人就是我。
首先,你的代码并不总是产生1,2,3,4。只需检查一下: http : //ideone.com/ohtt0正确的代码至少是这样的 :
void foo(int a, int b, int c, int d)
{
int* p = &a;
for (int i = 0; i < 4; i++)
{
std::cout << *p;
p++;
}
}
现在让我们尝试使用fastcall
, 这里 :
void __attribute__((fastcall)) foo(int a, int b, int c, int d)
{
int* p = &a;
for (int i = 0; i < 4; i++)
{
std::cout << *p << " ";
p++;
}
}
int main()
{
foo(1,2,3,4);
}
结果很乱: 1 -1216913420 134514560 134514524
所以我真的怀疑这里可以保证一些东西。
C ++标准没有调用约定的概念。 这是由编译器处理的。
在这种情况下,如果标准要求在应用address-of运算符时参数是连续的,那么标准对编译器的要求与您对它的要求之间存在冲突。
由编译器决定做什么。 但是,我认为大多数编译器会将您的要求优先于标准。
标准中没有关于调用约定或如何传递参数的内容。
确实,如果你获取一个必须存储在内存中的变量(或参数)的地址。 它没有说该值不能在寄存器中传递,然后在其地址被存储时存储到存储器中。
它肯定不会影响其他变量,不会采用谁的地址。
你的基本假设是有缺陷的。 在我的机器上, foo(1,2,3,4)
使用你的代码打印出来:
1 -680135568 32767 4196336
在64位x86上使用g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.