472,784 Members | 881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

Eight-byte alignment

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
19 3978
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
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
<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
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
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
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
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
“ 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

"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

"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
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
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
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
Ⓥ_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

"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

<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cfanatic | last post by:
Hey all, I been reading through these forums for a long time but have never posted. Well, I got a dillema. I have a program of the Eight Queen's program and I have to make it work without...
9
by: jblazi | last post by:
In the book "Algortims and Data Structures" by Wirth there is a program in Pascal to compute all 92 solutions of the above mentioned problem. I tried to translate his program into C++, the only...
5
by: Matt | last post by:
I will be grateful if someone explians this part colfree = FALSE; upfree = FALSE; downfree = FALSE; of the code below. I don't understand how this marks the downward and upward diagonals....
2
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
1
by: aidy | last post by:
Hi, I have x amount of rows in a db2 table. I want to update an account number column with a random eight digit number: This is where I have got UPDATE zzz2 SET AC = RND(/not sure what...
4
by: ravi | last post by:
C/C++ program to find a possible solution of eight queen problem Can any one help me to get the solution ?
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.