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

struct size confusion:

Hello:

I am relatively new to Python and this is my first post on
this mailing list.

I am confused as to why I am getting size differences in the following
cases:
print struct.calcsize("I") 4 print struct.calcsize("H") 2 print struct.calcsize("HI") 8 print struct.calcsize("IH")

6

Why is it 8 bytes in the third case and why would it be only 6 bytes
in the last case if it is 8 in the previous?

I tried specifying big endian and little endian and they both have
the same results.

I suspect, there is some kind of padding involved, but it does not
seem to be done consistently or in a recognizable method.

I will be reading shorts and longs sent from C into Python
through a socket.

Suppose I know I am getting 34 bytes, and the last 6 bytes are a 2-byte
word followed by a 4-byte int, how can I be assured that it will be in that
format?

In a test, I am sending data in this format:
PACK_FORMAT = "HBBBBHBBBBBBBBBBBBBBBBBBBBHI"
which is 34 bytes
However, when I receive the data, I am using the format:
UNPACK_FORMAT = "!HBBBBHBBBBBBBBBBBBBBBBBBBBHHI"
which has the extra H in the second to last position to make them
compatible, but that makes it 36 bytes. I am trying to come up with
some explanation as to where the extra 2 bytes come from.

Thanks in advance:

Mar 22 '06 #1
2 2265
Michael Yanowitz wrote:
Why is it 8 bytes in the third case and why would it be only 6 bytes
in the last case if it is 8 in the previous?


From TFM:

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

See this how to achieve the desired results (on my system at least):
print struct.calcsize("I") 4 print struct.calcsize("H") 2 print struct.calcsize("HI") 8 print struct.calcsize("=HI") 6 print struct.calcsize("=IH") 6


Regards,

Diez
Mar 22 '06 #2
Michael Yanowitz wrote:
Hello:

I am relatively new to Python and this is my first post on
this mailing list.

I am confused as to why I am getting size differences in the following
cases:

print struct.calcsize("I")
4
print struct.calcsize("H")
2
print struct.calcsize("HI")
8
print struct.calcsize("IH")

6

Why is it 8 bytes in the third case and why would it be only 6 bytes
in the last case if it is 8 in the previous?


By default the struct module uses native byte-order and alignment which
may insert padding. In your case, the integer is forced to start on a
4-byte boundary so two pad bytes must be inserted between the short and
the int. When the int is first no padding is needed - the short starts
on a 2-byte boundary.

To eliminate the padding you should use any of the options that specify
'standard' alignment instead of native:

In [2]: struct.calcsize('I')
Out[2]: 4

In [3]: struct.calcsize('H')
Out[3]: 2

In [4]: struct.calcsize('HI')
Out[4]: 8

In [5]: struct.calcsize('IH')
Out[5]: 6

In [6]: struct.calcsize('!HI')
Out[6]: 6

In [7]: struct.calcsize('>HI')
Out[7]: 6

In [8]: struct.calcsize('<HI')
Out[8]: 6
I tried specifying big endian and little endian and they both have
the same results.
Are you sure? They should use standard alignment as in the example above.
I suspect, there is some kind of padding involved, but it does not
seem to be done consistently or in a recognizable method.

I will be reading shorts and longs sent from C into Python
through a socket.

Suppose I know I am getting 34 bytes, and the last 6 bytes are a 2-byte
word followed by a 4-byte int, how can I be assured that it will be in that
format?

In a test, I am sending data in this format:
PACK_FORMAT = "HBBBBHBBBBBBBBBBBBBBBBBBBBHI"
which is 34 bytes


Are you sure? Not for me:

In [9]: struct.calcsize('HBBBBHBBBBBBBBBBBBBBBBBBBBHI')
Out[9]: 36

Kent
Mar 22 '06 #3

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

Similar topics

5
by: Russell Shaw | last post by:
Hi, In setjmp.h on a linux system, there is: typedef struct __jmp_buf_tag { ... } jmp_buf; I could understand typedef struct { } jmp_buf, but how do i interpret typedef struct { } jmp_buf ?
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
7
by: venkatbo | last post by:
Hi folks, Of TurboGers & Django WAF candidates, which one would be easier to use in an environment where the data/content doesn't come an RDBMS, but from other server-side apps... If these are...
3
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
37
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested...
9
by: hankssong | last post by:
hi all, I'm wondering how to locate the C header file where defines some struct variable,macro and function. In the MSDN, we can easily solve it by using INDEX search; but how we can do it in the...
9
by: royend | last post by:
Hi. I am having trouble with the definition of my two structs in C++. Both have functions depending on each other, which means none of them can be declared before the other. How may I solve this?...
8
by: Guillaume Dargaud | last post by:
I just saw the following two lines in /usr/include/usb.h and my head is spinning: struct usb_dev_handle; typedef struct usb_dev_handle usb_dev_handle; What is that supposed to mean ? I...
7
by: vaneric001 | last post by:
hi I am a newbie to c coding and going thru K&R ... i have a structure to represent an image as struct image{ char* imagename; int imagewidth; int imageheight; long* pixels; };
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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
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...
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,...
0
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...

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.