Skip to content

Issue in sizeof() function #79743

Closed
Closed

Description

@amiraslanhaghrah
BPO 35562
Nosy @ammaraskar, @AmirAslanHaghrah

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-12-22.17:19:36.313>
created_at = <Date 2018-12-22.17:07:08.926>
labels = ['invalid', 'ctypes', 'type-bug', '3.7']
title = 'Issue in sizeof() function'
updated_at = <Date 2018-12-22.18:03:23.491>
user = 'https://github.com/amiraslanhaghrah'

bugs.python.org fields:

activity = <Date 2018-12-22.18:03:23.491>
actor = 'ammar2'
assignee = 'none'
closed = True
closed_date = <Date 2018-12-22.17:19:36.313>
closer = 'ammar2'
components = ['ctypes']
creation = <Date 2018-12-22.17:07:08.926>
creator = 'Amir Aslan Haghrah'
dependencies = []
files = []
hgrepos = []
issue_num = 35562
keywords = []
message_count = 4.0
messages = ['332355', '332357', '332358', '332360']
nosy_count = 2.0
nosy_names = ['ammar2', 'Amir Aslan Haghrah']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue35562'
versions = ['Python 3.7']

Activity

  1. amiraslanhaghrah commented on Dec 22, 2018

    amiraslanhaghrahmannequin
    MannequinAuthor

    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.

  2. ammaraskar commented on Dec 22, 2018

    @ammaraskar
    Member

    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.

  3. amiraslanhaghrah commented on Dec 22, 2018

    amiraslanhaghrahmannequin
    MannequinAuthor

    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?

  4. ammaraskar commented on Dec 22, 2018

    @ammaraskar
    Member

    Iterating over _fields_ and adding up the sizeof() for each type could be one solution.

  5. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions