Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python大小的ctypes与`sys.getsizeof(Var)`方法vs`ctypes.sizeof(Var)`_Python_Size_Byte_Ctypes_Sys - Fatal编程技术网

python大小的ctypes与`sys.getsizeof(Var)`方法vs`ctypes.sizeof(Var)`

python大小的ctypes与`sys.getsizeof(Var)`方法vs`ctypes.sizeof(Var)`,python,size,byte,ctypes,sys,Python,Size,Byte,Ctypes,Sys,我有一个关于python中可变大小的问题,我使用Ctypes是因为我想要一个1字节的数字,但是当我试图在python中检查它的大小时(通过sys.getsize),它说它是80字节,但当我用Ctypes检查时(通过Ctypes.sizeof),它说它只有1字节,有人能告诉我有什么不同吗?为什么有两种不同的尺寸?这是因为python使用了对象或包装器吗?当它发送到c时,它会查看真实的大小 import sys import ctypes print("size in ctypes is : ",

我有一个关于python中可变大小的问题,我使用Ctypes是因为我想要一个1字节的数字,但是当我试图在python中检查它的大小时(通过
sys.getsize
),它说它是80字节,但当我用Ctypes检查时(通过
Ctypes.sizeof
),它说它只有1字节,有人能告诉我有什么不同吗?为什么有两种不同的尺寸?这是因为python使用了对象或包装器吗?当它发送到c时,它会查看真实的大小

import sys
import ctypes

print("size in ctypes is : ",ctypes.sizeof(ctypes.c_byte(1)))
print("size in sys is : ",sys.getsizeof(ctypes.c_byte(1)))
导致

size in ctypes is :  1
size in sys is :  80

sys.getsizeof
返回python对象的大小,这与C数据类型的大小无关。这一点您不必担心。

ctypes模块用于在Python中创建和操作C数据类型。 这就是为什么ctypes.sizeof(ctypes.c_字节(1))返回1

>>> import sys
>>> help(sys.getsizeof) Help on built-in function getsizeof in module sys:

getsizeof(...)
    getsizeof(object, default) -> int

    Return the size of object in bytes.


如果你想知道细节,你应该看看(尤其是文件顶部的注释)。您的
ctypes.c_字节(1)
是一个Python对象:

>>> import sys
>>> import ctypes
>>> isinstance(ctypes.c_byte(1), object)
True
正如@Daniel所指出的,
sys.getsizeof
获取Python对象的大小。该Python对象比C中对应的对象大。请注意
object.h
注释中的以下内容:

Objects are structures allocated on the heap. . . .
The actual memory allocated for an object
contains other data that can only be accessed after casting the pointer
to a pointer to a longer structure type.  This longer type must start
with the reference count and type fields; the macro PyObject_HEAD should be
used for this.
换句话说,宏
PyObject\u HEAD
附加到每个对象的开头。这会增加Python对象的大小

另一方面,
ctypes.sizeof
返回Python对象中
C
数据类型的实际大小(使用C的
sizeof
运算符)

编辑

根据您在Daniel的帖子评论中提到的目标,可以在Python3.x中通过服务器发送一个字节。下面是一个示例,说明如何使用Python的
socket
模块发送字节来证明这一点

以下是服务器,您将在一个Python解释器中运行该服务器:

# Server
import socket

HOST = ''                      # All available interfaces
PORT = 50007                   # Same port as client
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1)        # receive data with bufsize 1; a larger bufsize will break this code
    if not data: break
    conn.sendall(data)
conn.close()
# Client
import socket

HOST = '127.0.0.1'             # The remote host, but here using localhost
PORT = 50007                   # The port used by both the client and server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'1')                # a bytes object
data = s.recv(1)               # Receive data from the socket with bufsize of 1
s.close()
print('Received', repr(data))  # confirm receipt
以下是客户端,您将在另一个python解释器中运行该客户端:

# Server
import socket

HOST = ''                      # All available interfaces
PORT = 50007                   # Same port as client
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1)        # receive data with bufsize 1; a larger bufsize will break this code
    if not data: break
    conn.sendall(data)
conn.close()
# Client
import socket

HOST = '127.0.0.1'             # The remote host, but here using localhost
PORT = 50007                   # The port used by both the client and server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'1')                # a bytes object
data = s.recv(1)               # Receive data from the socket with bufsize of 1
s.close()
print('Received', repr(data))  # confirm receipt

我有一个赋值,我应该通过UDP数据包只发送2个字节,这有点令人沮丧,因为我找不到在变量中只存储两个字节的方法,prolly不应该为这种赋值选择python?但是python不可能无法完成这项任务。如何发送字节?只需使用bytestring。ctypes仅用于与其他语言的外国库进行通信。是的,我猜这是在我提到对象包装器时发生的,因此只有当它传递给实际的C代码时,它才会算作ctypes大小?@Newbie是的,在一定程度上。我在Daniel的帖子下看到了您的评论,您可以通过Python3.x实现您的目标。我会更新我的帖子,告诉你怎么做。非常感谢,我以前确实试过这个,但没有用,一定是我的代码出了问题,因为你的代码工作得很好:)谢谢@新手很乐意帮忙,我也很高兴你也这么想。干杯