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

c doughts

P: 1
what is the padding in c?
Jun 13 '07 #1
Share this Question
Share on Google+
6 Replies


100+
P: 208
what is the padding in c?
This should go in the Forums not in the articles
Jun 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
I am moving this to the C/C++ Forum.

Padding is a term applied to the alignment of the variables in your code. Most compilers align on an int boundary. If you have a struct:

Expand|Select|Wrap|Line Numbers
  1. struct test
  2. {
  3.      int a;
  4.      char b;
  5.      int c;
  6. };
  7.  
the compiler will align the ints on an int boundary. To do that, three pad bytes will be inserted after member b. Normally, you need no worry about padding as it is handled internally by the compiler.
Jun 13 '07 #3

100+
P: 208
Expand|Select|Wrap|Line Numbers
  1. struct test
  2. {
  3.      int a;
  4.      char b;
  5.      int c;
  6. };
  7.  
It's also the reason why:
Expand|Select|Wrap|Line Numbers
  1. sizeof(test) != sizeof(a) + sizeof(b) + sizeof(c) 
Jun 13 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
It's also the reason why:

Code: ( c )
sizeof(test) != sizeof(a) + sizeof(b) + sizeof(c)
Yep. I remember when I first did that. To quote Bugs Bunny, "Nyah, it don't add up, doc".
Jun 13 '07 #5

P: 1
how much space does an object in c++ eats up when it is declared/initialised?[/b]
Jun 13 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
[quote=abhielementx786]

how much space does an object in c++ eats up when it is declared/initialised?

[/qoute]
The same as a struct since classes are implemented as a struct. The one difference is that if the struct/class contains a virtual function, the object will contain the address of the virtual function table. In this case the C++ object is larger by the sizeof a pointer.
Jun 14 '07 #7

Post your reply

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