This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Issue in sizeof() function
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Amir Aslan Haghrah, ammar2
Priority: normal Keywords:

Created on 2018-12-22 17:07 by Amir Aslan Haghrah, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg332355 - (view) Author: Amir Aslan Haghrah (Amir Aslan Haghrah) Date: 2018-12-22 17:07
If you define a structure which contains an 'c_int' and a 'c_double'  member. Then run the sizeof() function for it you get 16 as result as follows:

---------------------------------------------
from ctypes import c_int
from ctypes import c_double
from ctypes import sizeof
from ctypes import Structure
from struct import Struct

class issueInSizeof(Structure):
    _fields_ = [('KEY',     c_int),                
                ('VALUE',   c_double)]

print(sizeof(issueInSizeof))

---------------------------------------------
output:
16
---------------------------------------------

In continue if you add another 'c_int' to your structure and run sizeof() function as follows. It return 16 too.

---------------------------------------------
from ctypes import c_int
from ctypes import c_double
from ctypes import sizeof
from ctypes import Structure
from struct import Struct

class issueInSizeof(Structure):
    _fields_ = [('Index',   c_int),   
                ('KEY',     c_int),              
                ('VALUE',   c_double)]

print(sizeof(issueInSizeof))

---------------------------------------------
output:
16
---------------------------------------------

If python assume the size of 'c_int' 4, then it should return 12 in the first run. Also if it assume the size of 'c_int' 8 then it should return 24 in the second run.

thanks in advance.
msg332357 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2018-12-22 17:19
This has to do with C struct packing and alignment. You are likely on a 64-bit computer and thus your structs are aligned to 8 byte (64 bit) boundaries.

https://docs.python.org/2/library/ctypes.html#structure-union-alignment-and-byte-order

Create an equivalent C program and you will notice the same behavior.
msg332358 - (view) Author: Amir Aslan Haghrah (Amir Aslan Haghrah) Date: 2018-12-22 17:43
Thank you for your response.

I noticed this issue while working with 'mmap' and memory sharing.
As you said I check it in 'C++' and I got the same result for my struct as Python.
---------------------------------------------------------------------
I used the 'sizeof' function to calculate my struct size to read it from mapped memory and I get wrong output in reading memory because of this aligning procedure.

Question: Structs map to memory unaligned but sizeof return the aligned size of struct.
How we can calculate the sizeof struct to use it in reading from memory?
msg332360 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2018-12-22 18:03
Iterating over _fields_ and adding up the sizeof() for each type could be one solution.
History
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79743
2018-12-22 18:03:23ammar2setmessages: + msg332360
2018-12-22 17:43:51Amir Aslan Haghrahsetmessages: + msg332358
2018-12-22 17:19:36ammar2setstatus: open -> closed

nosy: + ammar2
messages: + msg332357

resolution: not a bug
stage: resolved
2018-12-22 17:07:08Amir Aslan Haghrahcreate