468,512 Members | 1,530 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

what is memory padding ?

Hi what is memory padding in C ?
Oct 13 '06 #1
2 11334
D_C
293 100+
The basic answer is that all 4 byte variables must line up on boundaries divisible by 4, 2 byte variables must line up on boundaries divisible by 2, and 1 byte boundaries must line up on boundaries divisible by 1 (not ever a problem).

For example, storing a struct with bytes that go
1 4 1 2 vs. 4 2 1 1.
The first one takes 1 + 3 padding + 4 + 1 + 1 padding + 2 = 12 bytes (4 bytes of padding).
The second way takes 4 + 2 + 1 + 1 = 8 bytes (no padding)

There are similar threads with similar examples, but I didn't see any from the last week. You would have to dig past page 5 in this forum to find such a post. I think search only searches the archives.
Oct 13 '06 #2
Banfa
9,056 Expert Mod 8TB
The basic answer is that all 4 byte variables must line up on boundaries divisible by 4, 2 byte variables must line up on boundaries divisible by 2, and 1 byte boundaries must line up on boundaries divisible by 1 (not ever a problem).
While this scheme is very common where variables of various sizes must be aligned in memory is actually platform dependent. I know of platforms(68000 IIRC) where 4 byte variables only have to be aligned on a 2 byte boundary, and structure padding can get very complex in some of the more inteligent compilers.
Oct 13 '06 #3

Post your reply

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

Similar topics

15 posts views Thread by damian birchler | last post: by
15 posts views Thread by puzzlecracker | last post: by
2 posts views Thread by RupeshDeenadayalan | last post: by
11 posts views Thread by simonp | last post: by
8 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
5 posts views Thread by .rhavin grobert | last post: by
43 posts views Thread by bharath539 | last post: by
66 posts views Thread by Why Tea | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.