473,396 Members | 2,010 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,396 software developers and data experts.

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 1830
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
1
by: andychambers2002 | last post by:
Hi All, A C library I'm using has a number of functions that all require a struct as an argument. The example module shows how to make a new Python Object from C code and I've seen other posts...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible...
0
by: Keith | last post by:
Hello, I am trying to create exectuables on inux using "pyinstaller". I am using pyinstaller-1.3, RHEL 4.4, Python 2.5. The executables fail to run. The problem returned is pertaining to...
0
by: abarun22 | last post by:
Hi I am facing a problem while including a C header file in the SWIG interface file. However the problem does not occur when i directly copy the contents of header file in the same place. My...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
8
by: Bryan.Fodness | last post by:
Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. Here is my function that takes in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.