Structure padding explained
Related Blog Items
What is Padding?
Aligning members of a structre to a byte boundary. Padding is done by compiler to gain the performance. Most hardware architectures
access addresses fast if they are aligned properly, otherwise there will be performance penalty. In order to increase performance compiler pads structures.
How Padding done?
Normally processors require members of the structure aligned to byte boundary, for instance char member(1 byte) should be aligned to
one byte boundary that is it can appear at any byte boundary, a short int(2 bytes) should be aligned to 2 byte boundary that is it should appear at 2 byte boundary, long int(4 bytes) should be aligned to 4 byte boundary. If we declare a basic data type in structure it should appear at sizeof(basic data type) byte boundary, in order to make the start address of the next member at an aligned address.
Example:
Struct X
{
Char a;
Short int b;
Char c;
Long
d;
};
| a(1) | P(1)| b(2) | c(1)| P(3) | d(4) |
P - padding bytes
Why Padding?
If the processor is serious about alignment it raises an exception/signal (ex. SIGBUS), otherwise there will be a performance penalty
will be there as misalignment slowdown data access.
Are members order going to be changed?
ANSI C requires fields of a structure need to allocated in the order they are declared.
As a programmer how does padding matter to me?
It matters if you are concerned about space it takes. By changing the members order of declarations, some amount space can be saved. For example
Struct X
{
Char a;
Long
Int b;
Char c;
Long int d;
Char e;
Long int f;
};
Sizeof(struct X) = 24 bytes
Lets reorder the above structure as below
Struct X
{
Char a;
Char c;
Char e;
Long Int b;
Long int
d;
Long int f;
};
Sizeof(struct X) = 16 bytes
Padding is only done to members or to the structure as a whole
also?
Padding will be done by compiler to structure’s members and to the structure as a whole also. Compiler pads structure as whole because this allows each member of structure aligned in array of structures.
Packed structures
Some compilers provide #pragma to suppress padding or to make it packed to n number of bytes. Some provide keywords to do this.
Generally pragma which is used for modifying structure padding will be in the below format (depends on compiler)
#pragma pack(n)
For example arm
provides __packed keyword to suppress structure padding. Go through your compiler manual to know more about this.
So a packed structure is a structure without padding.
Generally packed structures will be used
->to save space, and
->to format a data structure to transmit over network using some protocol (this is not a good practice of course because you need to deal
with endianness)
How to declare array of packed structures?
We can not declare array of packed structures as for declaring as an array we need each member of structure aligned. So alternatively either we can declare something like this
Struct X
{
Unsigned char a;
Unsigned long b;
}array[NUM_ELEMENTS];
The above structure yields compilation error, if we declare this as below, we should be able to handle..
Unsigned char array_bytes[sizeof(structure) * NUM_ELEMENTS];
And take special care while handling array_bytes.
When I need to be careful about structure padding?
1)when writing structures to a file on one machine and reading them on different machine
This involves problem, consider this example
Struct X
{
Char a;
Long int b;
};
x.a = 0×01;
x.b = 0×02;
If you write struct X to a file, it would have written 0×01 G G G 0×00 0×00 0×00 0×02 (G is garbled value, because this area is padded)
While reading this on different machine(could be big endian) we read the structure as whole, on big-endian these values will be altered and as a result we read corrupted value for member a;
Solution for this would be using a packed structures we can eliminate this.
How can I pack structures Using GCC?
Using the "packed" attribute against the members of a structure. This attribute mechanism is an extension to the GNU C compiler. An example of how you would do this is below.
struct X
{
unsigned char a __attribute__((__packed__));
unsigned
short b __attribute__((__packed__));
unsigned long c
__attribute__((__packed__));
};
Another way to achieve the same is as shown below:
struct X
{
unsigned char a;
unsigned short b;
unsigned long c;
}
__attribute__((__packed__));
How can I pack structures Using ARM compiler?
__packed keyword is used for this purpose, usage is shown below
__packed struct X
{
char a;
Long int b;
};
May be in some other post, I try to give details about how padding done for
global variables, pointers and etc. on some selected machines. Your comments,
suggestions, criticism, etc. most welcome.
Popularity: 38%
You need to log on to convert this article into PDF
Related Blog Items
Related Blog Items
KKoool Stuff……….
January 9th, 2007 at 2:15 pm
IeriWinner_83…
HI! I’ve have similar topic at my blog! Please check it..
Thanks.
[url=http://www.google.com][/url]
http://www.google.com…
February 14th, 2007 at 1:24 pm