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

Re: struct.pack behavior

P: n/a
On 25Jun2008 22:38, Steven Clark <st************@gmail.comwrote:
| On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sj******@lexicon.netwrote:
| On Jun 26, 9:00 am, "Steven Clark" <
| >Can anyone explain to me why
| >struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2)
| >gives 4 bytes?
| >>
| Alignment -- read the manual.
| If "the manual" is the help files for the struct module, I've read it
| several times over. I understand endianness; I don't understand
| alignment. Could anyone give a less cryptic / terse answer?

For efficiency reasons many CPUs require particular primitive data
types (integers/pointers of various sizes) to be placed in memory at
particular boundaries. For example, shorts ("H" above, usually two bytes
and probably always so in the struct module) are often required to be
on even addresses, and longer objects to be on 4 or 8 byte boundaries.

This allows for much more efficient memory access on many platforms
(of course the rules depend on the platform). Although RAM _appears_ to
the random access to arbitrary bytes, the underlying hardware will often
fetch chunks of bytes in parallel. If a number spanned the boundaries of
such a chunk it would require two fetch cycles instead of one. So
this is avoided for performance reasons.

So, packing "HB" puts a short at offset 0 (even) and then a byte.
Conversely, packing "BH" puts a byte at offset zero but puts the short
at offset 2 (to be even), leaving a gap after the byte to achieve this,
thus the 4 byte size of the result (byte, gap, short).

This layout procedure is called "alignment".

Cameron Simpson <

Kilimanjaro is a pretty tricky climb. Most of it's up, until you reach the
very, very top, and then it tends to slope away rather sharply.
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.