2014-02-07 2 views
0

구조체의 크기를 얻는 방법? sys.getsizeof()을 사용했지만 원하는 출력을 제공하지 않습니다.구조체의 크기를 얻는 데 혼란이 있음

는의 코드 아래 생각해 보자 :

#using bit fields for storing variables 
from ctypes import * 
def MALE(): 
    return 0 
def FEMALE(): 
    return 1 
def SINGLE(): 
    return 0 
def MARRIED(): 
    return 1 
def DIVORCED(): 
    return 2 
def WIDOWED(): 
    return 3 
class employee(Structure): 
    _fields_= [("gender",c_short, 1),       #1 bit size for storage 
       ("mar_stat", c_short, 2),       #2 bit size for storage 
       ("hobby",c_short, 3),        #3 bit size for storage 
       ("scheme",c_short, 4)]       #4 bit size for storage 
e=employee() 
e.gender=MALE() 
e.mar_status=DIVORCED() 
e.hobby=5 
e.scheme=9 
print "Gender=%d\n" % (e.gender) 
print "Marital status=%d\n" % (e.mar_status) 
import sys 
print "Bytes occupied by e=%d\n" % (sys.getsizeof(e)) 

출력 : 내가 Bytes occupies by e=2

이에 대한 모든 솔루션을 원하는

Gender=0 

Marital status=2 

Bytes occupied by e=80 

?

답변

3

ctypes.sizeofsys.getsizeof은 동일하지 않습니다. 전자는 C 구조의 크기를, 후자는 파이썬 객체 래퍼의 크기를 제공합니다.

0

C structctypes.Structure 개체를 비교할 수 없습니다. 마지막 하나는 그 컴패니언 c보다 훨씬 많은 정보를 담고있는 파이썬 객체이다. struct.

관련 문제