Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am aware of structure padding in C/C++ done by compiler for data alignment. Padding takes unnecessary memory.

But if I am not expecting a padding from compiler then where I can face a problem because of padding. Can anybody provide and example that can cause a serious bug in program because of padding.

Thanks in advance!
Vicky
Posted
Updated 8-Jun-10 23:07pm
v2

Provided you don't make any false assumptions about what the compiler is producing, you'll be fine.

No problems will occur by accessing the members of a struct by name, but if pointer arithmetic is employed there could be some issues.

Note that sizeof will always return the actual size of the struct and not the sum of the members making up the struct, so stepping to the next item in an array using pointers will always be safe as long the pointer is of the type of the struct or if the distance stepped is equal to the sizeof of the struct.

If, however, you're using offsets from the start of a struct to the address of a member you could run into problems. Consider the following struct:

struct my_struct
{
   char a;
   int  b;
}

Such a struct could be padded by the compiler to something like:

struct my_struct
{
   char a;
   char padding;
   int  b;
}


Therefore, if you have a statement like:

my_struct ms;


accessing b by an offset of sizeof(a) from the start of, &ms + sizeof(char), will not necessarily end up at the beginning of b.

Hope this helps.
 
Share this answer
 
The only time structure padding (and alignment) is important is if you're generating data that is transferred to something that makes different assumptions about the padding.

So if you have memory mapped hardware you might get a surprise if you naively map a structure over the hardware's base address, especially if it mixes registers of different sizes.

Another place you could get problems is transferring data over a network to a computer with a different word size or alignment requirements. For example an x86 can read and write to any byte address, most RISC processors can't (or can't in their default mode).

So if you intend doing any hardware or network programming (including RPC/COM/CORBA) you probably have to be aware of the issue (or use data types with fixed sizes on either side). For most programming it doesn't really matter.

Cheers,

Ash
 
Share this answer
 
Many compilers will come with ways to set the alignment size either globally or for specific structures. Setting this to one will remove that padding. Check your compiler documentation for this. For example here is the appropriate documentation for GCC: GCC Attribute Documentation[^].


However, it is generally considered bad form to write a program that depends on such things. It is probably far better to rewrite your program so that is doesn't rely on a structure not being padding, this will save you work in the long run.

Charles Keepax
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900