2016-02-06 65 views
1

我正在使用pyOpenGL,并且OpenGL要求我通过传递一个指针和要传输的字节数来传输数据。(ctypes.c_int * len(x))(* x)是做什么的?

我知道python不会像c那样在内存中存储变量。我发现下面的代码,使我的工作方案:

x = [1, 2, ... ]    # some list 
(ctypes.c_int * len(x))(*x) 

但是我不知道为什么它的工作原理(我不只是要相信我没有刚刚得到幸运的一切是如何掉到记忆)。这段代码实际上在做什么?

+0

你没有提到你使用的是什么这是有效的,但是你可以使用'sizeof'来获得要传输的字节数。例如:'cx =(ctypes.c_int * len(x))(* x);''num_bytes = ctypes.sizeof(cx)'。 – eryksun

+0

我假设你将数组作为C函数参数传递。这是可行的,因为ctypes数组就像C数组一样,作为指向第一个元素的指针传递。 – eryksun

+0

是的,它被作为C函数参数传递。 – Francis

回答

4

按照Python documentation

的推荐方法来创建具体数组类型是乘以 具有正整数的任何ctypes的数据类型。或者,您可以 这个类型的子类和定义长度类型类变量。 可以使用标准下标和 切片存取读取和写入数组元素;对于片段读取而言,结果对象本身不是 阵列。

实施例:

>>> from ctypes import * 
>>> TenIntegers = c_int * 10 
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 
>>> print ii 
<c_long_Array_10 object at 0x...> 
>>> for i in ii: print i, 
... 
1 2 3 4 5 6 7 8 9 10 
>>> 

因此,第一部分ctypes.c_int * len(x)创建一个数组类型与len(x)元素:

In [17]: ctypes.c_int * 10 
Out[17]: __main__.c_int_Array_10 

In [18]: ctypes.c_int * 100 
Out[18]: __main__.c_int_Array_100 

类型创建之后,你应该调用它并传递数组元素:

(ctypes.c_int * len(x))(*x) 
#      ^^^^ 

创建的数组类型接受元素的可变参数个数,这样,你应该expand list x using the *x form

In [24]: x = [1, 2, 3] 

In [25]: (ctypes.c_int * len(x))(*x) 
Out[25]: <__main__.c_int_Array_3 at 0x7f0b34171ae8> 

In [26]: list((ctypes.c_int * len(x))(*x)) 
Out[26]: [1, 2, 3] 

In [27]: (ctypes.c_int * len(x))(*x)[1] 
Out[27]: 2 

你不可错过x,因为__init__预计整数:

In [28]: (ctypes.c_int * len(x))(x) 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-28-ff45cb7481e4> in <module>() 
----> 1 (ctypes.c_int * len(x))(x) 

TypeError: an integer is required (got type list)