Solved

Structure Alignment

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

864 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

25 Experts available now in Live!

Get 1:1 Help Now