Solved

Structure Alignment

Posted on 1998-08-20
2
353 Views
Last Modified: 2010-04-15
What is Structure alignment? How does it affect performance?
Why can not two dlls having different structure alignments
share their structures?
0
Comment
Question by:krpadman
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 100 total points
ID: 1252206
Answer is comming
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1252207
To answer your questions I will use this struct as an example:
struct Something{
          char aChar;
          short int aShortInt;
          long aLong;
          double aDouble;
} Something;
sizeof (char)=1 Byte
sizeof (short int)=2 Byte
sizeof (long)=4 Byte
sizeof (double)=8 Byte
1 Byte = 8 Bits
Raw size of Something =15 Byte
>>What is Structure alignment?
Structure alignmet is how the compiler organizes the memory for a structure.
With No Alignment sizeof(something)=15
with 4 byte Alignment sizeof(Something)=20:
struct Something{
          char aChar;
          char Gap1[3];
          short int aShortInt;
          char Gap2[2]
          long aLong;
          double aDouble;
} Something;
tha gaps are invisible to your program. That means you can't write
struct Something Some;
Some.Gap1[1]='X';
>>How does it affect performance?
Each computer has a Preferred Adressing mode depending on its bus width
A computer with a bus width of 16 or 32 bit can access even adresses faster than odd adresses. Some computers even can only fetch 16 bit values so accessing 8 bit values on odd adresses is a 16 bit fetch on a the Odd Adress-1 and a swap of the bytes. To Speed up the program the compiler can fill in gaps
to move the elments to even 16 bit or 32 bit adresses:

>>Why can not two dlls having different structure alignments
share their structures?
That is because the position inside the memory is different.
Remember a Struct is only a logical thing to tell the compiler how to interpret the data on a given memory adress. The compiler adresses the a member of a struct by calculating baseAdress of struct + offset to member
so adressing the aLong member will calculate with no aligment as
Base+3 and with 4 byte alignment as Base+8

I hope i have answered your question fullfilled
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now