cytpes **int

VernM Vern.Muhr at gmail.com
Tue Apr 29 16:01:22 EDT 2008


On Apr 28, 11:57 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> Gabriel Genellina schrieb:
>
>
>
>
>
> > [snip repetition]
>
> > That's true for "a pointer to a pointer to int", and it's valid if the
> > functions references **b or b[0][0] - but in this case int** probably
> > means "[pointer to] an array of arrays of int" and presumibly the
> > function will try to access b[3][2] (or whatever indices are in range).
> > The duality pointer/array in C is dangerous when defining interfases -
> > you have to know how the value is intended to be accessed.
> > (I assume the function modifies the integer values, but not the pointers
> > themselves)
>
> > # build an array of 10x10 ints
> > Arr10int = c_int * 10
> > Pint = POINTER(c_int)
> > PPint = POINTER(Pint)
> > Arr10pint = Pint * 10
> > a = Arr10pint()
> > for i in range(10):
> >     a[i] = Arr10int()
> > ... initialize the array ...
> > ... load the function ...
> > # call the function
> > somefunction.argtypes = (PPint,)
> > somefunction.restype = None
> > somefunction(a)
>
> Yup, you are right - I somehow missed the access description and thought
> of the pointer-to-a-pointer as out-parameter-spec.
>
> Diez- Hide quoted text -
>
> - Show quoted text -


To provide a little more detail: currently, a DLL function uses malloc
to create a pointer to a block of memory where the ints are stored
This pointer is returned to python. Then a pointer to this pointer is
passed to another C function which manipulates the ints. When that C
function returns, python needs to access the int values. I am now able
to get this to work with this grossly ugly code.

    # Setup a place to store the *int pointer
    pt = (ctypes.c_int * 1)
    cube = pt()
    cube[0] = dll.AllocCube() # Get the pointer to the ints

    # Call the function that manipulates the ints
    dll.FirstPrime(ctypes.byref(cube))

    # Create a python list of the ints
    result = [ctypes.c_int.from_address(cube[0]+i*4).value for i in
range(5)]

I appreciate the suggestions so far. I know there must be a cleaner
way to express this. I would prefer the array of ints to be built by
cytpes, rather than by a C function in the DLL.







More information about the Python-list mailing list