八字节对齐 [英] Eight-byte alignment
问题描述
编译器是否保证下面的变量w放在
八字节对齐的地址上?
void myFunction(long iFreq)
{
const double w = two_pi * iFreq;
...
...
}
Does a compiler guarantee that the variable w below is placed on an
eight-byte aligned address?
void myFunction( long iFreq )
{
const double w = two_pi * iFreq;
...
...
}
推荐答案
gl **** @ hotmail.com 写道:
编译器是否保证下面的变量w放在
八字节对齐的地址上?
Does a compiler guarantee that the variable w below is placed on an
eight-byte aligned address?
编号对于任何对象,C ++标准WRT特定对齐没有要求
。
你会发现每个编译器/平台在这个意义上是不同的
和许多编译器(如果不是全部)有一种方法可以控制
对齐边界。
No. There are no requirement in C++ Standard WRT specific alignment
for any objects.
You will find that every compiler/platform is different in that sense
and that many compilers (if not all) have a way for you to control the
alignment boundary.
void myFunction(long iFreq)
{
const double w = two_pi * iFreq;
...
...
}
void myFunction( long iFreq )
{
const double w = two_pi * iFreq;
...
...
}
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要'不要问
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Re:这段代码与你的相似,GLChin:
a ?? main(){const double w = 0; } a ??,
地址w ??是8字节对齐。
使用VC ++ 8,你可以检查自己,如下:
#pragma warning(禁用:4007 4189 4430 4508 )
WinMain(int,int,int,int){
const __int32 Int32 = 0; const __int64 Int64 = 0;
int Implicit_Size_of_Int32 = int(& Int64) - int(& Int32);
//在这里打破,一个? ? Implicit_Size_of_Int32 == 8 a ??
}
Re: This code, similar to yours, GLChin:
a?? main() { const double w = 0 ; } a??,
The address of a?? w a?? is 8-byte-aligned.
Using VC++ 8, you can check for yourself, like this:
#pragma warning( disable: 4007 4189 4430 4508 )
WinMain( int, int, int, int ) {
const __int32 Int32 = 0 ; const __int64 Int64 = 0 ;
int Implicit_Size_of_Int32 = int( & Int64 ) - int( & Int32 );
// Breaking here, a?? Implicit_Size_of_Int32 == 8 a??.
}
< gl **** @ hotmail.comwrote in message
新闻:6d ********************************** @ b40g2000 prf.googlegroups。 com ...
<gl****@hotmail.comwrote in message
news:6d**********************************@b40g2000 prf.googlegroups.com...
编译器是否保证下面的变量w放在
八字节对齐的地址上?
void myFunction(long iFreq)
{
const double w = two_pi * iFreq;
.. 。
...
}
Does a compiler guarantee that the variable w below is placed on an
eight-byte aligned address?
void myFunction( long iFreq )
{
const double w = two_pi * iFreq;
...
...
}
是的,编译器始终保证变量默认对齐
正确的类型。由于这是一个双倍,它将是8字节
对齐。因为这是一个const double,所以没有要求
编译器为它分配任何内存 - 它可以被注册或
在任何地方重新计算。在实践中,编译器可能会为它分配
内存,它将是8字节对齐的。
唯一一次内存不能对齐的是当你使用#pragma pack,一个
的内存对齐命令行选项,或完成指针运算
你自己不尊重这种类型'对齐。
-cd
Yes, the compiler always guarantees that variables are by default aligned
correctly for their type. Since this is a double, it will be 8-byte
aligned. Since this is a const double, there''s no requirement that the
compiler allocate any memory for it at all - it could be enregistered or
re-calculated wherever used. In practice, the compiler probably assigns
memory for it, which would be 8-byte aligned.
The only time memory won''t be aligned is when you''ve used #pragma pack, one
of the memory alignment command-line options, or done pointer arithmetic
yourself that doesn''t honor the type''s alignment.
-cd
这篇关于八字节对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!