[英]Wrapping C++ dynamic array with Python+ctypes, segfault
我想包装一个小的C ++代码,用ctypes分配一个数组,并且将地址存储在c_void_p对象中有问题。
(注意:有意地将指针强制转换为void*
,因为后来我也希望以相同的方式对C ++对象的数组进行分配。)
要包装的C(++)函数:
void* test_alloc()
{
const int size = 100000000;
int* ptr = new int[size];
std::cout << "Allocated " << size * sizeof(int) << " bytes @ " <<
ptr << std::endl;
return static_cast<void*>(ptr);
}
void test_dealloc(void* ptr)
{
int* iptr = static_cast<int*>(ptr);
std::cout << "Trying to free array @ " << iptr << std::endl;
delete[] iptr;
}
Python包装器(假设以前的函数已经使用ctypes导入):
class TestAlloc(object):
def __init__(self):
self.pointer = ctypes.c_void_p(test_alloc())
print "self.pointer points to ", hex(self.pointer.value)
def __del__(self):
test_dealloc(self.pointer)
对于小数组(例如size = 10),似乎没问题:
In [5]: t = TestAlloc()
Allocated 40 bytes @ 0x1f20ef0
self.pointer points to 0x1f20ef0
In [6]: del t
Trying to free array @ 0x1f20ef0
但是如果我想分配一个大的(大小= 100 000 000),就会出现问题:
In [2]: t = TestAlloc()
Allocated 400000000 bytes @ 0x7faec3b71010
self.pointer points to 0xffffffffc3b71010L
In [3]: del t
Trying to free array @ 0xffffffffc3b71010
Segmentation fault
存储在ctypes.c_void_p中的地址显然是错误的,高4字节无效。 不知何故,32位和64位地址是混合的,并且在大数组分配的情况下,内存管理器(在这种情况下)被强制返回一个在32位(thx TonJ)上无法表示的地址。
有人可以为此提供解决方法吗?
该代码已使用g ++ 4.4.3编译,并在带有4G RAM的Ubuntu Linux 10.04 x86_64上运行。 Python版本是2.6.5。
非常感谢你!
更新:
我设法解决了这个问题。 我忘了为test_alloc()
指定restype。 restype的默认值是ctypes.c_int
,64位地址不适合。 通过在调用test_alloc()
之前添加test_alloc.restype = ctypes.c_void_p
解决了问题。
从它看来,似乎问题不在于小/大阵列分配,而是在32位和64位地址的混合中。 在您的示例中,小数组的地址适合32位,但大数组的地址不适合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.