473,748 Members | 2,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4137
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_o f_Int32 = int( & Int64 ) - int( & Int32 );
// Breaking here, “ Implicit_Size_o f_Int32 == 8 ”.
}

Dec 3 '07 #3
<gl****@hotmail .comwrote in message
news:6d******** *************** ***********@b40 g2000prf.google groups.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.nos pam>
wrote in message news:uV******** ******@TK2MSFTN GP02.phx.gbl...
<gl****@hotmail .comwrote in message
news:6d******** *************** ***********@b40 g2000prf.google groups.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 objektorientier ter Datenverarbeitu ng
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_remov e_this_and_nos. ..@mvps.org.nos pamwrote:
<glc...@hotmail .comwrote in message
news:6d******** *************** ***********@b40 g2000prf.google groups.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 objektorientier ter Datenverarbeitu ng
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...@Yaho o.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_o f_Int32 = int( & Int64 ) - int( & Int32 );
// Breaking here, ? Implicit_Size_o f_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 objektorientier ter Datenverarbeitu ng
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*******@Yaho o.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 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6856
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 recursion. I have to use a stack instead. It's been buggin me for a week and I have no clue how to start it. Do you guy think you can help me out? Here is the recursive part of the program:
9
4788
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 small problem being that he uses array with a starting index other than zero. Here is my solution: // Pascal like array with arbitrary indeces class Array { int *datum,start_index,end_index;
5
6745
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. How does downfree mark the diagonal?
2
18023
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
3223
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
4775
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 goes in here/] WHERE AC LIKE ''
4
9334
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 ?
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.