473,386 Members | 1,785 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,386 software developers and data experts.

Re: Struct usage and varying sizes of h, l, etc

Ethan Furman wrote:
Greetings,

I'm looking at the struct module for binary packing of ints and floats.
The documentation refers to C datatypes. It's been many years since I
looked at C, but I seem to remember that the data type sizes were not
fixed -- for example, an int might be two byes on one machine, and four
bytes on the next. Can any C programmers verify this? If it is true,
does that mean that struct.pack('h', 8001) might give me different
results depending on the machine it's running on?
Right. I believe (but could be wrong) that "char" is defined to be one byte, but
that "short", "int", "long", and "long long" are defined as "at least as big as
the previous type".

In practice, though, on nearly every machine that Python runs on, "char" is one
byte, "short" is two bytes, and "int" is four bytes. "longs" and "long longs"
tend to vary substantially, though; never assume sizes for them.

Single-precision floats are always four bytes and double-precision floats are
always eight bytes. "long doubles" vary; they could be twelve bytes or sixteen.

If you want to deal with fixed sizes, use struct.calcsize() to test the sizes of
each of the integer types, assign them to width-specific aliases, and always use
these aliases.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '08 #1
4 1038
On May 20, 5:59*pm, Robert Kern <robert.k...@gmail.comwrote:
Ethan Furman wrote:
Greetings,
I'm looking at the struct module for binary packing of ints and floats. *
The documentation refers to C datatypes. *It's been many years since I
looked at C, but I seem to remember that the data type sizes were not
fixed -- for example, an int might be two byes on one machine, and four
bytes on the next. *Can any C programmers verify this? *If it is true,
does that mean that struct.pack('h', 8001) might give me different
results depending on the machine it's running on?

Right. I believe (but could be wrong) that "char" is defined to be one byte, but
that "short", "int", "long", and "long long" are defined as "at least as big as
the previous type".
There are also minimum sizes for each type:

char: 8 bits
short: 16 bits
int: 16 bits
long: 32 bits
long long: 64 bits
In practice, though, on nearly every machine that Python runs on, "char" is one
byte, "short" is two bytes, and "int" is four bytes. "longs" and "long longs"
tend to vary substantially, though; never assume sizes for them.

Single-precision floats are always four bytes and double-precision floats are
always eight bytes. "long doubles" vary; they could be twelve bytes or sixteen.

If you want to deal with fixed sizes, use struct.calcsize() to test the sizes of
each of the integer types, assign them to width-specific aliases, and always use
these aliases.
Also, according to the documentation for the struct module, the =, <,
and format characters specify "std. size" rather than "native size".
Jun 27 '08 #2
On 2008-05-20, Robert Kern <ro*********@gmail.comwrote:
>I'm looking at the struct module for binary packing of ints
and floats. The documentation refers to C datatypes. It's
been many years since I looked at C, but I seem to remember
that the data type sizes were not fixed -- for example, an int
might be two byes on one machine, and four bytes on the next.
Can any C programmers verify this? If it is true, does that
mean that struct.pack('h', 8001) might give me different
results depending on the machine it's running on?

Right. I believe (but could be wrong) that "char" is defined
to be one byte,
Yes, C defines "char" to be one byte, but it doesn't define the
size of a "byte" other than it's at least big enough to hold
one character (or something like that). In practice, a byte is
pretty much guaranteed to be at least 8 bits. But, on some
targets a "byte" is 16 bits, and on others a byte is 32 bits.

However, I'm not aware of any Python implementations on those
targets...

--
Grant Edwards grante Yow! Clear the laundromat!!
at This whirl-o-matic just had
visi.com a nuclear meltdown!!
Jun 27 '08 #3
On May 21, 10:04 am, Grant Edwards <gra...@visi.comwrote:
Yes, C defines "char" to be one byte, but it doesn't define the
size of a "byte" other than it's at least big enough to hold
one character (or something like that). In practice, a byte is
pretty much guaranteed to be at least 8 bits.
If you're discussing standard C, you can omit "in practice" and
"pretty much". CHAR_BITS is required to be at least 8 by the ANSI/ISO
C Standard.
But, on some targets a "byte" is 16 bits, and on others a byte
is 32 bits.
Yep, or 9 bits or 36 bits, or anything >= 8.
Jun 27 '08 #4
Ethan Furman wrote:
Next question: when using struct.pack to store an integer, I get a
deprecation warning if the int is too big... I would rather have an
error. Is there a setting somewhere that controls this?
http://docs.python.org/dev/library/warnings

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 27 '08 #5

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

Similar topics

2
by: Mark McKay | last post by:
I have a thread which is used for updating a display window. The normal paint message queue is being bypassed in favor of drawing on demand by this thread. This thread is passed a Graphics...
6
by: FTSoJ | last post by:
On a file A.c I define the structure T1 typedef struct { int dummy; int age; char lastname ; char firstname; } T1_user; On a file B.c I define a strcuture T2
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
45
by: Borked Pseudo Mailed | last post by:
I have this weird problem with the JPEG library that I am using. There is a function that is being called and one of the parameters being passed is the length of a structure. Inside this...
2
by: kernel_panic | last post by:
Consider the following code noting the two uses of sizeof(): unsafe struct A { int value; } unsafe struct B { fixed byte value; // Error: 'A' does not have a
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
4
by: Ed | last post by:
Hi, guys, In C language manner, we need to put a "struct" token before one struct variable declaration like following. <code> struct Apple { float Price; };
15
by: js5895 | last post by:
hi, when I move to the next entrie by adding to the pointers address, I'm off by 6 bytes. Then I changed my code so my struct would have an extra member, pad, and then it was off by 9 bytes....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.