1

Couple of issues with python struct. Please let me know what is correct.

  1. Document mentions length of l/L as 4 but when checked with calcsize it gives 8.

    >>> struct.calcsize('l')
    8
    
  2. struct module calcsize is giving wrong size. If individual element size is calculated, it's sum is 90 but when calculated together with calcsize it gives 92.

    >>> struct.calcsize('8s2sIII30s32s6s')
    92
    >>> struct.calcsize('8s')
    8
    >>> struct.calcsize('2s')
    2
    >>> struct.calcsize('III')
    12
    >>> struct.calcsize('30s')
    30
    >>> struct.calcsize('32s')
    32
    >>> struct.calcsize('6s')
    6
    
jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
asked Sep 5, 2017 at 6:18
2
  • 4
    1. "The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent." 2. Because of padding. Commented Sep 5, 2017 at 6:22
  • Understood. I should have paid more attention to documentation. @jonrsharpe please add this as answer. I would like to accept this answer. Commented Sep 5, 2017 at 6:34

1 Answer 1

6

Elaborating answer posted by jonrsharpe in comments.

  1. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of '<', '>', '!' or '='. When using native size, the size of the packed value is platform-dependent.

    >>> struct.calcsize('l')
    8
    >>> struct.calcsize('=l')
    4
    
  2. Because of padding. Use = to not use padding.

    >>> struct.calcsize('=8s2sIII30s32s6s')
    90
    
answered Sep 5, 2017 at 8:55
Sign up to request clarification or add additional context in comments.

Comments

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.