[issue35562] Issue in sizeof() function

2018-12-22 Thread Amir Aslan Haghrah


New submission from Amir Aslan Haghrah :

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.

--
components: ctypes
messages: 332355
nosy: Amir Aslan Haghrah
priority: normal
severity: normal
status: open
title: Issue in sizeof() function
type: behavior
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue35562>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35562] Issue in sizeof() function

2018-12-22 Thread Amir Aslan Haghrah


Amir Aslan Haghrah  added the comment:

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?

--

___
Python tracker 
<https://bugs.python.org/issue35562>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com