Solved

Structure Alignment

Posted on 1998-08-20
2
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
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.

734 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