0

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.

asked Oct 10, 2022 at 6:30
1
  • 2
    Guessing it's padding. If you have a member with an alignment of 4 bytes, the struct is usually padded to end on a 4-byte alignment. Commented Oct 10, 2022 at 6:32

1 Answer 1

1

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.

answered Oct 10, 2022 at 7:03
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant. I wish this was clearer and more boldly stated in those docs than it is.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.