469,610 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Problem with 'struct' module

Using the 'struct' module (Win32, python version 2.4.1)--

The library documentation says that 'no alignment is required for any
type'. However, struct.calcsize('fd') gives 16 while
struct.calcsize('df') gives 12, implying that double precision data
has to start on a double-word boundary.

Matt Feinstein

--
There is no virtue in believing something that can be proved to be true.
Jul 19 '05 #1
2 1598
On 2005-06-14, Matt Feinstein <no****@here.com> wrote:
Using the 'struct' module (Win32, python version 2.4.1)--

The library documentation says that 'no alignment is required
for any type'.
Right. It says that for Standard alignment, and that's correct.
However, struct.calcsize('fd') gives 16 while
struct.calcsize('df') gives 12, implying that double precision
data has to start on a double-word boundary.
Your example is not using standard alignment. It's using
native alignment:

By default, C numbers are represented in the machine's native
format and byte order, and properly aligned by skipping pad
bytes if necessary (according to the rules used by the C
compiler).

Alternatively, the first character of the format string can
be used to indicate the byte order, size and alignment of
the packed data, according to the following table:

Character Byte order Size and alignment
@ native native
= native standard
< little-endian standard big-endian standard

! network (= big-endian) standard

If the first character is not one of these, "@" is assumed.

Native byte order is big-endian or little-endian, depending
on the host system. For example, Motorola and Sun
processors are big-endian; Intel and DEC processors are
little-endian.

Native size and alignment are determined using the C compiler's
sizeof expression. This is always combined with native byte
order.

Standard size and alignment are as follows: no alignment is
required for any type (so you have to use pad bytes); short is
2 bytes; int and long are 4 bytes; long long (__int64 on
Windows) is 8 bytes; float and double are 32-bit and 64-bit
IEEE floating point numbers, respectively.

Note the difference between "@" and "=": both use native
byte order, but the size and alignment of the latter is
standardized.

--
Grant Edwards grante Yow! ... I want FORTY-TWO
at TRYNEL FLOATATION SYSTEMS
visi.com installed withinSIX AND A
HALF HOURS!!!
Jul 19 '05 #2
On Tue, 14 Jun 2005 14:24:56 -0000, Grant Edwards <gr****@visi.com>
wrote:

Your example is not using standard alignment. It's using
native alignment:

By default, C numbers are represented in the machine's native
format and byte order, and properly aligned by skipping pad
bytes if necessary (according to the rules used by the C
compiler).

Alternatively, the first character of the format string can
be used to indicate the byte order, size and alignment of
the packed data, according to the following table:

Character Byte order Size and alignment
@ native native
= native standard
< little-endian standard
> big-endian standard

! network (= big-endian) standard

If the first character is not one of these, "@" is assumed.

Native byte order is big-endian or little-endian, depending
on the host system. For example, Motorola and Sun
processors are big-endian; Intel and DEC processors are
little-endian.

Native size and alignment are determined using the C compiler's
sizeof expression. This is always combined with native byte
order.

Standard size and alignment are as follows: no alignment is
required for any type (so you have to use pad bytes); short is
2 bytes; int and long are 4 bytes; long long (__int64 on
Windows) is 8 bytes; float and double are 32-bit and 64-bit
IEEE floating point numbers, respectively.

Note the difference between "@" and "=": both use native
byte order, but the size and alignment of the latter is
standardized.


Thanks. I clearly missed the point of the explanation...

Matt Feinstein

--
There is no virtue in believing something that can be proved to be true.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Angelo Secchi | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
1 post views Thread by andychambers2002 | last post: by
2 posts views Thread by ajikoe | last post: by
2 posts views Thread by Jansson Christer | last post: by
reply views Thread by Keith | last post: by
reply views Thread by abarun22 | last post: by
5 posts views Thread by Neil Crighton | last post: by
8 posts views Thread by Bryan.Fodness | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.