Accessing the content of a variable array with ctypes

安稳与你 提交于 2020-01-01 11:37:07

问题


I use ctypes to access a file reading C function in python. As the read data is huge and unknown in size I use **float in C .

int read_file(const char *file,int *n_,int *m_,float **data_) {...}

The functions mallocs an 2d array, called data, of the appropriate size, here n and m, and copies the values to the referenced ones. See following snippet:

*data_ = data;
*n_ = n;
*m_ = m;

I access this function with the following python code:

p_data=POINTER(c_float)
n=c_int(0)
m=c_int(0)
filename='datasets/usps'
read_file(filename,byref(n),byref(m),byref(p_data))

Afterwards I try to acces p_data using contents, but I get only a single float value.

p_data.contents
c_float(-1.0)

My question is: How can I access data in python?

What do you recommended? Please don't hesitate to point out if I left something unclear!


回答1:


might be simpler to do the whole thing in python with the struct library. but if you're sold on ctypes (and I don't blame you, it's pretty cool):

#include <malloc.h>
void floatarr(int* n, float** f)
{
    int i;
    float* f2 = malloc(sizeof(float)*10);
    n[0] = 10;
    for (i=0;i<10;i++)
    { f2[i] = (i+1)/2.0; }
    f[0] = f2;
}

and then in python:

from ctypes import *

fd = cdll.LoadLibrary('float.dll')
fd.floatarr.argtypes = [POINTER(c_int),POINTER(POINTER(c_float))]

fpp = POINTER(c_float)()
ip = c_int(0)
fd.floatarr(pointer(ip),pointer(fpp))
print ip
print fpp[0]
print fpp[1]

the trick is that capitals POINTER makes a type and lowercase pointer makes a pointer to existing storage. you can use byref instead of pointer, they claim it's faster. I like pointer better because it's clearer what's happening.



来源:https://stackoverflow.com/questions/3620348/accessing-the-content-of-a-variable-array-with-ctypes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!