Home > Software engineering >  Python: struct.calcsize, erroneous results or misunderstanding?
Python: struct.calcsize, erroneous results or misunderstanding?

Time:10-11

This basically is the problem I am facing:

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.calcsize("h")
2
>>> struct.calcsize("l")
4
>>> struct.calcsize("hl")
8
>>>

I expect "hl" to occupy 6 bytes. 2 for the short and 4 for the long, and struct.calcsize confirms my premise for that, but adds them to get 8. What gives? Am I misunderstanding something here? Which has to be my first suspicion otherwise we're looking at nasty bug in struct.calcsize.

CodePudding user response:

This is alignment coming in. You can force non padding by adding = in front of your string:

struct.calcsize("hl")
8
struct.calcsize("=hl")
6

You can find that in the docs: No padding is added when using non-native size and alignment, e.g. with ‘<’, ‘>’, ‘=’, and ‘!’. in https://docs.python.org/3/library/struct.html Note depending on what you're doing, turning off the padding might have performance impact.

  • Related