Solved

Structure Alignment

Posted on 1998-08-20
2
356 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

820 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