Jump to content

  • Log In with Google      Sign In   
  • Create Account

structure padding


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
11 replies to this topic

#1 Cibressus   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 September 2004 - 01:29 PM

what exactly is structure padding, and what causes it. and why does this offset sizeof()?

Sponsor:

#2 Palidine   Members   -  Reputation: 1281

Like
0Likes
Like

Posted 12 September 2004 - 01:32 PM

structure padding makes structures a multiple of 32 bits. this is because your processor is super fast at dealing with things in those multiples and slower otherwise. it effects sizeof because the sizeof will be in multiples of 32 bits even if what you coded isn't. in general it's best to just let the padding be there. you will otherwise just slow down your app a bit. the amount of memory "lost" is probably insignificant for what you're doing.

there are ways to turn off the padding in your code, just google for it if you truly care.

-me

#3 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
0Likes
Like

Posted 12 September 2004 - 01:35 PM

Structure padding is the use of environment- and platform-specific options to "round out" the sizes of POD structures to, say, multiples of 4 bytes. It is problematic for sizeof because it is external to the code itself. It can also be problematic for data intended to be transmitted over a network.

#4 Cibressus   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 September 2004 - 01:39 PM

POD?

#5 Evil Steve   Members   -  Reputation: 1987

Like
0Likes
Like

Posted 12 September 2004 - 01:41 PM

Quote:
Original post by Cibressus
POD?

Plain Old Data

#6 Cibressus   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 September 2004 - 01:43 PM

how do i pad a structure, and more importantly, how do i rid myself of the padding so it can be passed through sizeof?

#7 Sneftel   Senior Moderators   -  Reputation: 1781

Like
0Likes
Like

Posted 12 September 2004 - 01:44 PM

Look here.

#8 Evil Steve   Members   -  Reputation: 1987

Like
0Likes
Like

Posted 12 September 2004 - 01:46 PM

Ok, heres an example:

struct Foo
{
int n1;
char ch1;
int n2;
};


Now, assuming the compiler pads to 4 bytes, and that an int is 4 bytes and a char is 1, that structure will be 12 bytes long. The first int takes up 4 bytes, and then the 5th byte is taken up by ch1. The compiler then puts 3 bytes of padding in after that, so that n2 is aligned to a 4 byte boundary. That should (in theory) make it faster to access n2.
However, if you're reading and writing this structure to a file, you probably don't want padding, since you expect to read 9 bytes - not 12. So you would disable padding for this structure.

#9 enigma1   Members   -  Reputation: 180

Like
0Likes
Like

Posted 12 September 2004 - 01:46 PM

You change the project options. With VC6 project settings->C/C++->code generation change the struct member aligment to 1.

#10 Washu   Senior Moderators   -  Reputation: 5421

Like
0Likes
Like

Posted 12 September 2004 - 02:01 PM

Sizeof will account for padding. Don't disable it as some systems require alignment, and worse of all, it prevents almost all optimizations that the compiler can perform.

#11 techgeek   Members   -  Reputation: 97

Like
-1Likes
Like

Posted 19 February 2013 - 01:38 AM

structure padding matters most when alignment matters, which helps in potential performance and avoids any possible bus errors, please refer 

http://www.refcode.net/2013/02/structure-padding-explained.html



#12 frob   Moderators   -  Reputation: 22709

Like
0Likes
Like

Posted 19 February 2013 - 02:12 AM

The thread was over in 2004. Please check the date before bringing back long-dead topics.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS