[SciPy-User] [SciPy-user] numpy and C

tinauser tinauser@libero...
Wed Jun 9 09:35:09 CDT 2010

Dear Charles,

thanks again for the replies.
Why do you say that is difficoult to free memory?
What I do is to allocate the memory(pyincref) before calling the Python
script. The Python script uses then a timer to call a C function to which
the allocated PyArrayObject (created with PyArray SimpleNew) is passed. In
C, the pointer of the PyArray is assigned to a pointer that points to a sort
of data buffer that is filled from a camera. The data buffer is allocated
When the python GUI is closed, I just decref my PyArrayObject, that I'm
basically using just to pass pointer values. 

Charles R Harris wrote:
> On Wed, Jun 9, 2010 at 7:46 AM, Charles R Harris
> <charlesr.harris@gmail.com>wrote:
>> On Wed, Jun 9, 2010 at 5:38 AM, tinauser <tinauser@libero.it> wrote:
>>> Dear Charles,
>>> thanks for the reply.
>>> The part of code causing the problem was exactly this
>>> Pymatout_img->data= cam_frame->data;
>>> where Pymatout is a PyArrayObject and cam_frame is a structure having a
>>> pointer to undefined char data.
>>> The code works all right if I recast in this way
>>> Pymatout_img->data= (char*)cam_frame->data;
>>> I'm not sure if this is allowed;I guessed it works because even if
>>> Pymatout_img->data is always a pointer to char, the PyArrayObject looks
>>> in
>>> ->descr->type_num to see what is the data type.
>> Numpy uses char* all over the place and later casts to the needed type,
>> it's the old way of doing void*. So your explicit cast is fine. For some
>> compilers, gcc for example, you also need to use a compiler flag to let
>> the
>> compiler know that you are going to do such things. In gcc the flag is
>> -fno-strict-aliasing but I don't think you need to worry about this in
>> VC.
>> <snip>
> That said, managing the data in this way can be problematic as you need to
> track alignment and worry about freeing of memory. You might want to look
> at
> PyArray SimpleNewFromData.
> Chuck
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

View this message in context: http://old.nabble.com/numpy-and-C-tp28767579p28831237.html
Sent from the Scipy-User mailing list archive at Nabble.com.

More information about the SciPy-User mailing list