473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Structure Alignment

Hi all,

I was having a discussion with a co-worker about alignment of structures in
memory, and we came across a question. We have a struct like the following:

struct Car
{
unsigned char VIN[17];
unsigned char Make[26];
unsigned char Model[26];
unsigned short Year;
...
}

The sizeof(struct) is 916 bytes. If we want to compile and run this code on
64-bit Windows do we need to add a 4-byte array to the struct to make it
align on an 8-byte boundary? Or will the compiler handle that for us?
Also, are there any other reasons we might want to 'force' it to line up on
an 8-byte boundary? For instance, if we want to call the function from
other languages, etc.?

Thanks
Jun 26 '06 #1
2 1320
> I was having a discussion with a co-worker about alignment of structures
in memory, and we came across a question. We have a struct like the
following:

struct Car
{
unsigned char VIN[17];
unsigned char Make[26];
unsigned char Model[26];
unsigned short Year;
...
}

The sizeof(struct) is 916 bytes. If we want to compile and run this code
on 64-bit Windows do we need to add a 4-byte array to the struct to make
it align on an 8-byte boundary?
No.
see here:
http://msdn2.microsoft.com/en-us/library/83ythb65.aspx
Or will the compiler handle that for us?
yes
Also, are there any other reasons we might want to 'force' it to line up
on an 8-byte boundary? For instance, if we want to call the function from
other languages, etc.?


You should not have to do this yourself unless you want to reserve fields
for future use and want to maintain binary compatibility.

What you might need to do is to explicitly set pack size for specific
structures.
One reason to do this would be if your dll is used in an environment where a
specific pack size is expected.
For example, I know that the LabVIEW language uses pack size 1 for
structures, instead of the default 8.

see this for more details:
http://msdn2.microsoft.com/en-us/library/2e70t5y1.aspx

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jun 26 '06 #2
Cool, thanks! I don't think we have a specific pack size here (I haven't
been through all the code for all these applications yet though), but I just
want to make sure we don't have to do any clunky stuff to prevent problems
as we start porting some of this stuff to 64-bit machines. Thanks!

"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
You should not have to do this yourself unless you want to reserve fields
for future use and want to maintain binary compatibility.

What you might need to do is to explicitly set pack size for specific
structures.
One reason to do this would be if your dll is used in an environment where
a specific pack size is expected.
For example, I know that the LabVIEW language uses pack size 1 for
structures, instead of the default 8.

see this for more details:
http://msdn2.microsoft.com/en-us/library/2e70t5y1.aspx

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Jun 26 '06 #3

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

Similar topics

6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
4
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler...
5
by: xmllmx | last post by:
Please forgive me for cross-posting. I've post this to microsoft.publoc.vc.mfc. But I can't get any response. Maybe only MFC- related topics are cared there. To begin with code: union XXX {...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.