By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

What is padding?explain in brief

P: 13
Plz tell me about padding in C language
Jun 15 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
This is a very vague question, and could mean a lot of things.
Please try to provide more information......
Jun 15 '07 #2

P: 13
This is a very vague question, and could mean a lot of things.
Please try to provide more information......
I mean padding in c lang
Jun 15 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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:
Expand|Select|Wrap|Line Numbers
  1. struct MyStruct
  2. {
  3.      int a;
  4.      char b;
  5.      int c;
  6. };
  7.  
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:
Expand|Select|Wrap|Line Numbers
  1. sizeof(MyStruct) != sizeof(a) + sizeof(b) + sizeof(c);
  2.  
Jun 15 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.