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

Charles R Harris charlesr.harris@gmail....
Wed Jun 9 09:23:34 CDT 2010

On Wed, Jun 9, 2010 at 7:46 AM, Charles R Harris

> 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100609/2e66086b/attachment-0001.html 

More information about the SciPy-User mailing list