ctypes使用的变量、指针、引用和buffer

程序如下,学习关注点见备注内容

# -*- coding: utf-8 -*-
from ctypes import *
import sys
print '-'*100
python_str = 'tests中国人' 中文占4字节
print 'python_string', python_str
print 'len:', len(python_str) 字符长度,中文占3个长度,不含类似于C语言的结束符
print 'getsizeof', sys.getsizeof(python_str)
# print 'byref', byref(python_str) # byref参数必须是ctypes类型
# print 'sizeof', sizeof(python_str) # sizeof参数必须是ctypes类型
print '-'*100
c_str_p = c_char_p(python_str)
print 'c_str_p', c_str_p
# print 'len:', len(c_str_p) # pointer没有len
print 'getsizeof', sys.getsizeof(c_str_p) 整个对象在python中占用的字节数
print 'sizeof', sizeof(c_str_p) 指针地址占用的字节数
print 'addressof', hex(addressof(c_str_p)) 纯地址
print 'byref', byref(c_str_p) 引用指针
print 'string_at', string_at(c_str_p) 获取内容
print 'string_at 0-4', string_at(c_str_p, 4) 获取内容
print '-'*100
c_str_buffer = c_buffer(python_str)
print 'c_str_buffer', c_str_buffer
print 'getsizeof', sys.getsizeof(c_str_buffer)
print 'sizeof', sizeof(c_str_buffer) 字节数, 一个中文字符占3个字节,一个英文字符占1个字节,结束符占一个字节
print 'addressof', hex(addressof(c_str_buffer)) 纯地址
print 'byref', byref(c_str_buffer) 引用指针
print 'c_str_buffer.value', c_str_buffer.value 获取内容
print 'c_str_buffer[:4]', c_str_buffer[:4] 截取内容
print '-'*100
c_num_long = c_long(0xfff)
print 'c_num_long', c_num_long 对象本身
print 'value', c_num_long.value 
print 'sizeof', sizeof(c_num_long)
print '-'*100
c_num_int = c_int(123)
print 'c_num_int', c_num_int 对象本身
print 'value', c_num_int.value 
print 'sizeof', sizeof(c_num_int)
print '-'*100
c_num_int64 = c_int64(123)
print 'c_num_int64', c_num_int64 对象本身
print 'value', c_num_int64.value 
print 'sizeof', sizeof(c_num_int64)

运行结果

C:\Python27\python.exe C:/code/cetc/engine/DistributedNode/test.py

----------------------------------------------------------------------------------------------------

python_string tests中国人

len: 14

getsizeof 47

----------------------------------------------------------------------------------------------------

c_str_p c_char_p('tests\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xba')

getsizeof 128

sizeof 8

addressof 0x24f8a10L

byref <cparam 'P' (00000000024F8A10)>

string_at tests中国人

string_at 0-4 test

----------------------------------------------------------------------------------------------------

c_str_buffer <ctypes.c_char_Array_15 object at 0x00000000024F8A48>

getsizeof 128

sizeof 15

addressof 0x24f8a90L

byref <cparam 'P' (00000000024F8A90)>

c_str_buffer.value tests中国人

c_str_buffer[:4] test

----------------------------------------------------------------------------------------------------

c_num_long c_long(4095)

value 4095

sizeof 4

----------------------------------------------------------------------------------------------------

c_num_int c_long(123)

value 123

sizeof 4

----------------------------------------------------------------------------------------------------

c_num_int64 c_longlong(123L)

value 123

sizeof 8

Process finished with exit code 0

好记性不如烂笔头

转载于:https://www.cnblogs.com/q916699319/p/6957006.html

使用ctypes返回结构体指针,你可以按照以下步骤进行操作: 1. 首先,你需要在Python中导入ctypes模块:`import ctypes` 2. 然后,你需要定义结构体的布局。你可以使用`ctypes.Structure`类来定义结构体,并在其中定义结构体的字段。例如,你可以按照以下方式定义一个与引用\[2\]中的结构体相对应的结构体: ```python class StructPointerTest(ctypes.Structure): _fields_ = \[ ("x", ctypes.c_int), ("y", ctypes.c_int) \] ``` 3. 接下来,你需要加载动态库。你可以使用`ctypes.CDLL`类来加载动态库。例如,如果你的动态库名为libtest.so,你可以按照以下方式加载它: ```python lib = ctypes.CDLL("libtest.so") ``` 4. 然后,你需要定义函数的返回类型和参数类型。对于返回结构体指针的函数,你可以使用`ctypes.POINTER`来定义返回类型。例如,你可以按照以下方式定义一个与引用\[3\]中的函数相对应的函数: ```python lib.test.restype = ctypes.POINTER(StructPointerTest) ``` 5. 最后,你可以调用函数并获取返回的结构体指针。例如,你可以按照以下方式调用函数并获取返回的结构体指针: ```python result = lib.test() ``` 请注意,你需要确保在使用完结构体指针后,释放内存以避免内存泄漏。你可以使用`ctypes.free`函数来释放结构体指针的内存。例如,你可以按照以下方式释放结构体指针的内存: ```python ctypes.free(result) ``` 综上所述,要使用ctypes返回结构体指针,你需要定义结构体的布局,加载动态库,定义函数的返回类型和参数类型,调用函数并获取返回的结构体指针,并在使用完结构体指针后释放内存。 #### 引用[.reference_title] - *1* [Python Ctypes结构体指针处理(函数参数,函数返回)](https://blog.csdn.net/JoeBlackzqq/article/details/10441017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Python Ctypes 结构体指针处理(函数参数,函数返回)](https://blog.csdn.net/chenqunan3231/article/details/100845973)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值