gpt4 book ai didi

c++ - 用 Python+ctypes、segfault 包装 C++ 动态数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:20:11 24 4
gpt4 key购买 nike

我想用 ctypes 包装一个分配数组的小 C++ 代码,但将地址存储在 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

但是如果我想分配一个大的(size = 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 位,但大数组的地址不适合。

关于c++ - 用 Python+ctypes、segfault 包装 C++ 动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3100554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com