473,790 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1847
On 2005-06-14, Matt Feinstein <no****@here.co m> 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.co m>
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
2064
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 I was suggested by the list to use the struct module to parse it using the following code in the hypothesis that for each records I have just two fields: import struct fmt='10s1960s' size=struct.calcsize(fmt)
2
5189
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 second name for better readable. After optimization, global variables might be misaligned because each global variables must be converted to 32 bits, but I do see that C/C++ Compiler do padding between variables. Struct does the same to do padding....
1
1885
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 that recommend this way of doing it. In this case though, it would seem easier if I could create the object in the Python code. This would require storing a pointer to an instance of the struct until a certain function is called.
2
4454
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 */ #include <time.h>
2
10772
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 documentation over known issues with Python 2.4.1 so I try this... Here's the thing:
0
1710
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 "struct.py" not being able to find the module "_struct". struct.py is located under /usr/local/lib/python-2.5/, and there is a _struct.o (no _struct.py anywhere) located under /usr/local/lib/ python-2.5/lib-dynload.
0
1730
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 interface file read as follows. /* interface file dep.i */ %module dep %{ #include "dep.h"
5
5166
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 of '\r' in the extracted file seems to fix the problem, but I can't find an easy solution for binary files. The code I'm using is something like: from zipfile import Zipfile z = Zipfile(open('zippedfile.zip'))
8
1946
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 a string. def parseSequence(data, start):
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10145
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.