By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,218 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,218 IT Pros & Developers. It's quick & easy.

Eight-byte alignment

P: n/a
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;
...
...
}
Dec 3 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
gl****@hotmail.com wrote:
Does a compiler guarantee that the variable w below is placed on an
eight-byte aligned address?
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;
...
...
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #2

P: n/a
Re: This code, similar to yours, GLChin:
“ main() { const double w = 0 ; } ”,

The address of “ w ” 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, “ Implicit_Size_of_Int32 == 8 ”.
}

Dec 3 '07 #3

P: n/a
<gl****@hotmail.comwrote in message
news:6d**********************************@b40g2000 prf.googlegroups.com...
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;
...
...
}
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
Dec 3 '07 #4

P: n/a
I'm not sure that in 32-bit environment there is a stack frame alignment
guarantee, other than 4 bytes, of course. Thus, any doubles might be
unaligned.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uV**************@TK2MSFTNGP02.phx.gbl...
<gl****@hotmail.comwrote in message
news:6d**********************************@b40g2000 prf.googlegroups.com...
>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;
...
...
}

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


Dec 4 '07 #5

P: n/a
On Dec 3, 6:22 pm, glc...@hotmail.com wrote:
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;
...
...
}
A compiler does, but I don't know if your using that compiler.
Certainly not all compilers do---it wouldn't make sense on a
machine where sizeof(double) is 6, for example, nor on a 16 bit
machine. Depending on the hardware, it might not even make
sense on a 32 bit machine (nor a 36 bit machine, for that
matter).

The real question is why do you care? The compiler will
guarantee that w meets whatever requirements the hardware makes
for effective access. And that's really all you care about.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Dec 4 '07 #6

P: n/a
On Dec 3, 7:43 pm, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@mvps.org.nospamwr ote:
<glc...@hotmail.comwrote in message
news:6d**********************************@b40g2000 prf.googlegroups.com...
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;
...
...
}
Yes, the compiler always guarantees that variables are by
default aligned correctly for their type.
Which on most 32 bit machines is any multiple of 4. On the one
48 bit machine I'm aware of, it would be a multiple of 6. On
the various 36 bit machines I've seen or heard of, it would be a
multiple of 4 as well. On the earlier 16 bit machines I
worked on, it would be multiple of 2, and on the 8 bit machines,
there were no alignment restrictions.
Since this is a double, it will be 8-byte aligned.
On a Sun Sparc, probably. On an Intel based 32 bit machine, I
doubt it, and on the older, 16 bit Intels, almost certainly not.
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,
Which is implementation defined. Maybe it starts a game of
packman.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Dec 4 '07 #7

P: n/a
On Dec 3, 7:07 pm, Jeff?Relf <Jeff_R...@Yahoo.COMwrote:
Re: This code, similar to yours, GLChin:
? main() { const double w = 0 ; } ?,
The address of ? w ? is 8-byte-aligned.
Using VC++ 8, you can check for yourself,
You can check whether it is 8 byte aligned in one particular
case, after whatever calls preceded it.
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, ? Implicit_Size_of_Int32 == 8 ?.
}
I'm not sure what that's supposed to check. It's not C++, so it
doesn't tell us anything about what C++ does. And I don't see
how it would be related to how VC++ would lay out a double.

FWIW: VC++ doesn't guarantee 8 byte alignment. All of my
compilers on Sparc do---perhaps because accessing a double at an
address which isn't 8 byte aligned will cause a core dump:-).
Curiously, g++ on both the Linux machine and the Windows
machines here (32 bit Intel) also seems to guarantee 8 byte
alignment.

You might try something like the following:
#include <iostream>

double two_pi = 6.28 ;

void
f( long ifreq )
{
double w = two_pi * ifreq ;
std::cout << &w << std::endl ;
}

void
g()
{
f( 20 ) ;
}

template< size_t N >
void
h()
{
char dummy[ N ] ;
f( 20 ) ;
}

template< size_t N >
void
i()
{
char dummy[ N ] ;
double w ;
std::cout << &w << std::endl ;
}

int
main()
{
f( 20 ) ;
g() ;
h< 1 >() ;
h< 2 >() ;
h< 3 >() ;
h< 4 >() ;
h< 5 >() ;
h< 6 >() ;
h< 7 >() ;
h< 8 >() ;
i< 1 >() ;
i< 2 >() ;
i< 3 >() ;
i< 4 >() ;
i< 5 >() ;
i< 6 >() ;
i< 7 >() ;
i< 8 >() ;
return 0 ;
}

If all of the addresses output are multiples of 8, it still
isn't guaranteed, but I'd guess that there is a very good chance
of it being true. If they aren't, of course, you know that it
isn't guaranteed.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Dec 4 '07 #8

P: n/a
“ VC++ 8.0 ” 8-byte-aligns a “ __int64 ”, same as a “ double ”.

The code I showed ( news:Je***************************@Cotse.NET )
is most definately VC++ 8.0,
complied and debuged using Visual Studio 2005.

Had you tried it yourself, Mr. Kanze, you'd know that;
but, naturally, you couldn't do that.

Dec 4 '07 #9

P: n/a

"Jeff?Relf" <Je*******@Yahoo.COMwrote in message
news:Je***************************@Cotse.NET...
>" VC++ 8.0 " 8-byte-aligns a " __int64 ", same as a " double ".

The code I showed ( news:Je***************************@Cotse.NET )
is most definately VC++ 8.0,
complied and debuged using Visual Studio 2005.

Had you tried it yourself, Mr. Kanze, you'd know that;
but, naturally, you couldn't do that.
Furthermore, your code demonstrates packing, not alignment.
Dec 4 '07 #10

P: n/a

"Jeff?Relf" <Je*******@Yahoo.COMwrote in message
news:Je***************************@Cotse.NET...
>" VC++ 8.0 " 8-byte-aligns a " __int64 ", same as a " double ".

The code I showed ( news:Je***************************@Cotse.NET )
is most definately VC++ 8.0,
complied and debuged using Visual Studio 2005.

Had you tried it yourself, Mr. Kanze, you'd know that;
but, naturally, you couldn't do that.
Jeff, your code shows nothing at all, because it is in the main function.
Stack alignment is dependent on the caller to some degree, if the caller
left the stack 4-byte aligned, then you would not have 8-byte alignment.
Dec 4 '07 #11

P: n/a
Re: This code of mine:
“ #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, “ Implicit_Size_of_Int32 == 8 ”.
} ”.

How do you ( Ben ) explain “ Implicit_Size_of_Int32 == 8 ”
if it's not a manifestation of 8-byte alignment ?

Remove the “ const ” terms, or change them to “ static ”,
and you still get the same results.

Dec 4 '07 #12

P: n/a
On Dec 4, 11:52 pm, Jeff?Relf <Jeff_R...@Yahoo.COMwrote:
? VC++ 8.0 ? 8-byte-aligns a ? __int64 ?, same as a ? double ?.
The code I showed (news:Je***************************@Cotse.NET)
is most definately VC++ 8.0,
complied and debuged using Visual Studio 2005.
Had you tried it yourself, Mr. Kanze, you'd know that;
but, naturally, you couldn't do that.
No I couldn't, because VC++ doesn't work on my machine (a Sun
Sparc). It's certainly not C++, and if you tried it with any
even halfway conformant compiler (e.g. g++ -std=c++98, or even
VC++, with the proper options), you'd know that.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Dec 5 '07 #13

P: n/a
gl****@hotmail.com schrieb:
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;
...
...
}
Some compilers do, some compilers don't.
Visual Studio 2005 does align that way, as long as you do not modify the
default alignment.

Have a look in the online help for "__alignof" and
"__declspec(align(n))" to find more information:
http://msdn2.microsoft.com/en-us/lib...f4(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

There is no portable way to guarantee a particular alignment. If you
really think you need one, you have to check the technical manuals of
all compilers that you need to support.

Norbert
Dec 5 '07 #14

P: n/a
Ⓥ_Ben_Voigt_C, _D71eGS.Airband.NET Phx.GBL
2.48 Hours, Outlook_O19, Dec 5, 2007, 11._1 A, BS99eu

Re: This VC++ 8 code of mine, without the “ /O2 ” compiler option:
“ #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, ‘ Implicit_Size_of_Int32 == 8 ’.
} ”.

Implicit_Size_of_Int32 is 8 --because <-- VC++ aligned Int64.
Yes, that created some padding, but that's not the issue.

As I recently said in “ news:Je***************************@Cotse.NET ”,
the “ /O2 ” compiler option f u c k s things up.

Dec 5 '07 #15

P: n/a

"Jeff?Relf" <Je*******@Yahoo.COMwrote in message
news:Je***************************@Cotse.NET...
?_Ben_Voigt_C, _D71eGS.Airband.NET Phx.GBL
2.48 Hours, Outlook_O19, Dec 5, 2007, 11._1 A, BS99eu

Re: This VC++ 8 code of mine, without the " /O2 " compiler option:
" #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, ' Implicit_Size_of_Int32 == 8 '.
} ".

Implicit_Size_of_Int32 is 8 --because <-- VC++ aligned Int64.
Yes, that created some padding, but that's not the issue.
Alignment creates padding, but padding does not create alignment.

By measuring padding, you have not measured alignment. Really.
>
As I recently said in " news:Je***************************@Cotse.NET ",
the " /O2 " compiler option f u c k s things up.
You mean it shows that your assertion that the VC++ compiler always aligns
to 8 byte boundaries is wrong.
Dec 5 '07 #16

P: n/a

<gl****@hotmail.comwrote in message
news:6d**********************************@b40g2000 prf.googlegroups.com...
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;
...
...
}
http://msdn2.microsoft.com/en-us/lib...ignment_topic2
Dec 5 '07 #17

P: n/a
Given VC++ 8.0 without so-called “ optimizations ” or packing rules,
You can't show me a __int64 static, local or const
that isn't aligned on an 8-byte-boundry. I rest my case.

Dec 6 '07 #18

P: n/a
On Dec 6, 4:13 am, Jeff?Relf <Jeff_R...@Yahoo.COMwrote:
Given VC++ 8.0 without so-called ? optimizations ? or packing rules,
You can't show me a __int64 static, local or const
that isn't aligned on an 8-byte-boundry. I rest my case.
aligne.cc:

#include <iostream>

double two_pi = 6.28 ;

void
f( long ifreq )
{
__int64 w = two_pi * ifreq ;
std::cout << &w << std::endl ;
}

void
g()
{
f( 20 ) ;
}

template< size_t N >
void
h()
{
char dummy[ N ] ;
f( 20 ) ;
}

template< size_t N >
void
i()
{
char dummy[ N ] ;
__int64 w ;
std::cout << &w << std::endl ;
}

int
main()
{
f( 20 ) ;
g() ;
h< 1 >() ;
h< 2 >() ;
h< 3 >() ;
h< 4 >() ;
h< 5 >() ;
h< 6 >() ;
h< 7 >() ;
h< 8 >() ;
i< 1 >() ;
i< 2 >() ;
i< 3 >() ;
i< 4 >() ;
i< 5 >() ;
i< 6 >() ;
i< 7 >() ;
i< 8 >() ;
return 0 ;
}

Compiled with:
cl /vmg /GR /Gy /EHs /J /nologo /D_CRT_SECURE_NO_DEPRECATE /MTd /
GS- /Zi /w /D_DEBUG align.cc

Output:
0012FF40
0012FF38
0012FF34
0012FF34
0012FF34
0012FF34
0012FF30
0012FF30
0012FF30
0012FF30
0012FF44
0012FF44
0012FF44
0012FF44
0012FF44
0012FF44
0012FF44
0012FF44

Note that the original question concerned double, and this is
just a quick edit of the program which I used to verify that
VC++ doesn't guarantee alignment of double. I don't know why
the discussion changed type---double is C++, __int64 isn't, and
VC++ is the only compiler at my disposition which will compile
the above. The fact remains that VC++ doesn't guarantee
alignment of more than 4, ever.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Dec 6 '07 #19

P: n/a
Although I couldn't get an unaligned “ __int64 ” in “ WinMain() ”,
this “ Ch ” parameter unaligns it:

“ #pragma warning( disable: 4100 4189 4430 4508 )
F( char Ch ) { __int64 Int64 = 0x1234567812345678 ;
// Breaking here, ‘ & Int64 == 0x0012fef4 ’
}

int _stdcall WinMain( int, int, int, int ) { F( 'A' ); } ”.

So you're right, James ( and the rest ),
VC++ 8.0 does not aways 8-byte-align __int64.

Dec 6 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.