Plz tell me about padding in C language
What is padding?explain in brief
Collapse
X
-
Tags: None
-
Originally posted by DeManThis is a very vague question, and could mean a lot of things.
Please try to provide more information.... ..Comment
-
Padding is used by the compiler to align your variables.
Processors work with words. A word represents the size of the processor registers. It would be convenient if data in memory were aligned on word (32-bit) boundaries otherwise it would take two operations to get a variable into a register instead of one.
Your compiler knows this and tries to optimize the layout of your variables.
For example:
[code=c]
struct MyStruct
{
int a;
char b;
int c;
};
[/code]
the compiler will add 3 pad bytes after b so that c is aligned on a 32-bit boundary. There may also be pad bytes added so that a is aligned on a 32-bit boundary.
Normally you don't have to worry about this. However, because of this padding, you may find that:
[code=c]
sizeof(MyStruct ) != sizeof(a) + sizeof(b) + sizeof(c);
[/code]Comment
Comment