Python struct problem

clipboard.png

Why struct.calcsize ("5id") is 32?

struct RateInfo
  {
   time_t            time;               
   int               open;              
   int               high,low,close;   
   double            vol;           
  };

requires a struct such as unpack . How should fmt be defined?

refer to
https://www.coder4.com/archiv...
python.org/3.6/library/struct.html-sharpformat-characters" rel= "nofollow noreferrer" > https://docs.python.org/3.6/l...

thank you!

Jul.03,2022

Brother, the following is for reference only:

< H2 > one. Why is struct.calcsize ("5id") 32 < / H2 > Members in
C structures can be of different data types, and the members are stored in the memory space of contiguous in the order in which they are defined. Unlike an array, the size of the structure is not a simple addition of all member sizes of . You need to consider the address alignment problem of the system when storing structure variables
.

take the structure of C language to illustrate:

fmt "5id" is equivalent to the following structure:

struct s
{
    int i1, i2, i3, i4, i5;
    double j;
};

first introduce a related concept-offset. Offset refers to the difference between the address of the member in the structure variable and the address of the structure variable. The structure size is equal to the offset of the last member plus the size of the last member. Obviously, the address of the first member of the structure variable is the first address of the structure variable.

so i1 , the first member of
, has an offset of 0.
the offset of the second member i2 is the offset of the first member plus the size of the first member (0,4), its value is 4
the offset of the third member i3 is the offset of the second member plus the size of the second member (44th), and the value is 8
the offset of the fourth member i4 is the offset of the third member plus the size of the third member (84th). Its value is 12
the offset of the fifth member i5 is the offset of the fourth member plus the size of the fourth member (12: 4), the value is 16
the offset of the sixth member j is the offset of the fifth member plus the size of the fifth member (16 / 4), the value is 20.

in fact, due to the requirement of address alignment when storing variables, the compiler will follow two principles when compiling the program:

1. The offset of the member in the structure volume variable must be an integer multiple of the member size (0 is considered to be an integer multiple of any number)
2. The structure body size must be an integral multiple of the size of all members.

compared with Article 1, the offsets of the first five members in the above example all meet the requirements, but the offset of the sixth member j is 20, which is not an integral multiple of its (double) size. When processing, the compiler will add 4 empty bytes after the second member, making the offset of the third member 24.

compared with Article 2, the size of the structure is equal to the offset of the last member plus its size, and the size calculated in the above example is 32, which meets the requirements.

so, struct.calcsize ("5id") is 32

refer to the article from this big shot: https://blog.csdn.net/csw_100...

< H2 > two. How to define fmt < / H2 >
Note: By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details.

by default, the result of packaging a given C language structure includes padding bytes to maintain the correct alignment of the C language types involved; similarly, alignment is considered when unpacking. This behavior is selected so that the bytes of the packaged structure correspond exactly to the layout in memory of the corresponding C structure. To handle platform-independent data formats or omit implicit padding bytes, use standard size and alignment instead of native size and alignment: for more information, see python.org/3.6/library/struct.html-sharpstruct-alignment" rel=" nofollow noreferrer > byte order, size and alignment .

I checked time_t , it should be long integer
reference from: detailed introduction of time_t data types in C language
Let's tell you the answer first:

fmt = '@5id'

then how to choose the first character of the format string

clipboard.png
Source: python.org/3.6/library/struct.html-sharpstruct-alignment" rel=" nofollow noreferrer "> python.org/3.6/library/struct.html-sharpstruct-alignment" rel= "nofollow noreferrer" > https://docs.python.org/3.6/l...

Let me just talk about my understanding:

  • @ is the most commonly used. If you need to populate it, be sure to choose it
  • .
  • The sorting of = format characters may have an impact on size, but if you don't want the order to affect, use it, but it won't help you fill in the alignment
  • ! is mostly used for network bytes, and I don't understand it either (I hope the boss can help explain it. Welcome to edit directly), and I won't help you fill in the alignment
  • .
  • > Native byte order is big-endian , I don't know much about it, and it won't help you fill in the alignment
  • < Native byte order is little-endian , I don't know much about it, and it won't help you fill in the alignment
this is the official ! description
! is available for those poor souls who claim they can't remember whether network byte order is big-endian or little-endian.
'!' Applies to networks that claim to be unable to remember whether the network byte order is big-endian or little-endian

this is the usage of < , on the official website:
the native byte order is big-endian or little-endian, depending on the host system.
for example:

  • Intel x86 and AMD64 (x86-64) are little-endian ;
  • Motorola 68000 and PowerPC G5 are both big-endian ;
  • ARM and Intel Itanium have switchable byte order (bi-endian) .

use sys.byteorder to check your system's byte order.

There is no way to indicate non-native byte order (force byte-swapping);
use the appropriate choice of or > .
cannot confirm the use of non-native bytes or >

reference from:
python.org/3/library/struct.html-sharpmodule-struct" rel=" nofollow noreferrer "> struct official documentation : python.org/3/library/struct.html-sharpmodule-struct" rel= "nofollow noreferrer" > https://docs.python.org/3/lib...

Menu