[Pyrex] Allocating an array.array of a specific length in pyrex

Chris Lambacher lambacck at gmail.com
Wed Jun 8 20:28:59 CEST 2005


The memory is not temporary, I am passing it out as an array, thus the
malloc/free route will require double allocation(once for malloc/free,
once for array.array).  I am not using string because when I tried to
pass the data in as string, pyrex complained about the conversion. 
However, I could probably use the same PyObject_AsReadBuffer function
call to get at the data passed in to the function as a string and then
pass it out as a string as well using the interface suggested (
PyString_FromStringAndSize and PyString_AsString ).  Perhapse I need
to substitute PyObject_AsWriteBuffer instead of PyString_AsString in
order for my changes to get propegated back to the string object?

-Chris


On 6/8/05, Adam Langley <alangley at gmail.com> wrote:
> On 6/8/05, Chris Lambacher <lambacck at gmail.com> wrote:
> > My question is, is there a better way to
> > allocate the memory for the array.array object than:
> >       a = array.array('B', [0] * (pixels * 2))
> 
> cdef unsigned char *buffer
> temp_string = PyString_FromStringAndSize(NULL, length)
> buffer = <unsigned char *> PyString_AsString(temp_string)
> 
> That's one way to do it. But it looks like you just want an area of
> memory so why not:
> 
> cdef extern from "stdlib.h":
>   ctypedef unsigned long size_t
>   void *malloc(size_t size)
>   void free(void *mem)
> 
> ?
> 
> AGL
> 
> --
> Adam Langley                                      agl at imperialviolet.org
> http://www.imperialviolet.org                       (+44) (0)7906 332512
> PGP: 9113   256A   CC0F   71A6   4C84   5087   CDA5   52DF   2CB6   3D60
> 


-- 
Christopher Lambacher
lambacck at computer.org



More information about the Pyrex mailing list