Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > struct.pack behavior

Reply
Thread Tools

struct.pack behavior

 
 
Steven Clark
Guest
Posts: n/a
 
      06-25-2008
Can anyone explain to me why
struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2)
gives 4 bytes?

-Steven
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      06-25-2008
On Jun 26, 9:00 am, "Steven Clark" <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Steven Clark
Guest
Posts: n/a
 
      06-26-2008
On Wed, Jun 25, 2008 at 7:03 PM, John Machin <(E-Mail Removed)> wrote:
> On Jun 26, 9:00 am, "Steven Clark" <(E-Mail Removed)> wrote:
>> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


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?
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      06-26-2008
En Wed, 25 Jun 2008 23:38:54 -0300, Steven Clark
<(E-Mail Removed)> escribi�:

> On Wed, Jun 25, 2008 at 7:03 PM, John Machin <(E-Mail Removed)>
> wrote:
>> On Jun 26, 9:00 am, "Steven Clark" <(E-Mail Removed)> wrote:
>>> 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?


A good start might be this Wikipedia article:
<http://en.wikipedia.org/wiki/Data_structure_alignment>

--
Gabriel Genellina

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-26-2008
On Jun 26, 12:38*pm, "Steven Clark" <(E-Mail Removed)> wrote:
> On Wed, Jun 25, 2008 at 7:03 PM, John Machin <(E-Mail Removed)> wrote:
> > On Jun 26, 9:00 am, "Steven Clark" <(E-Mail Removed)> wrote:
> >> 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.
> > --
> >http://mail.python.org/mailman/listinfo/python-list

>
> 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?


google("struct alignment")
 
Reply With Quote
 
Steven Clark
Guest
Posts: n/a
 
      06-28-2008
> 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".
>
> Cheers,
> --
> Cameron Simpson <(E-Mail Removed)> DoD#743
> http://www.cskk.ezoshosting.com/cs/



Thanks for taking the time to type a detailed, helpful response,
Cameron. Much appreciated!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VERY odd routing behavior when attempting VPN connections over Wifi Robert Gordon Wireless Networking 0 08-25-2005 04:04 PM
Question about behavior of View Available Networks window mszablocky Wireless Networking 4 07-24-2005 08:28 PM
Firefox under Linux -- odd behavior Dennis J. Tuchler Firefox 0 07-28-2004 04:05 PM
[FF] middle button behavior Gillou Firefox 0 05-01-2004 08:46 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments