Solved

Structure Alignment

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

815 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

11 Experts available now in Live!

Get 1:1 Help Now