473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re: struct.pack behavior

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" <steven.p.cl...@gmail.comwrote:
| >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".

Cheers,
--
Cameron Simpson <cs@zip.com.auDoD#743
http://www.cskk.ezoshosting.com/cs/

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
0 942

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gordon Scott | last post by:
Hi All, I've got a problem I'm seeing when trying to use the struct module to send data to a different machine. Actually I'm making a condensed file that gets transferred to and read on a BREW...
6
by: Jack | last post by:
Hi, All, is it possible to send a struct using the the send function. Here is what I mean typedef struct{ int ID; char name; }sampleStruct; int main(){
5
by: Steven Clark | last post by:
Can anyone explain to me why struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2) gives 4 bytes? -Steven
0
by: Cameron Simpson | last post by:
On 25Jun2008 22:38, Steven Clark <steven.p.clark@gmail.comwrote: | On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sjmachin@lexicon.netwrote: | On Jun 26, 9:00 am, "Steven Clark"...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.