Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing pointer to array from C to Python..and modifying same array in python?

Reply
Thread Tools

Passing pointer to array from C to Python..and modifying same array in python?

 
 
JW
Guest
Posts: n/a
 
      07-11-2003
Hi,


Below is a description of what I would like to do. Would someone tell
me if it is possible and if so, how?

I have an array (presumably 'large') that is mallocced in a C function
and its
values are initialized in that same function.
I would like to pass just the pointer to the beginning of the array
from this C function to a Pyton function.
Then the python method can individually access and MODIFY individual
members of the array.
After the call to the python method, the C function will see any
changes to the array.

Can this be done?
I know how to create a PyTuple_New and change each member of the C
array into a PyObject, then add all thos PyObjects to the Tuple,
before passing that PyTyple from C to Python, but this seems slow.

Thanks in advance for your help.
 
Reply With Quote
 
 
 
 
David Jeske
Guest
Posts: n/a
 
      07-12-2003
On Fri, Jul 11, 2003 at 02:06:10PM -0700, JW wrote:
> I have an array (presumably 'large') that is mallocced in a C
> function and its values are initialized in that same function. I
> would like to pass just the pointer to the beginning of the array
> from this C function to a Pyton function. Then the python method
> can individually access and MODIFY individual members of the array.
> After the call to the python method, the C function will see any
> changes to the array.


> Can this be done?


Of course this can be done, and there are a number of ways to do
it. Based on your implication that there are many more values put in
the array by the C code than will be accessed (or changed) by the
Python code, this method might make sense:

1) have the C function construct a new python object (i.e. the "wrapper")

2) put the real pointer to the C array into the wrapper as instance data

3) register two methods for the object "get()" and "set()", impelemented
in C as Python exported functions.

The get method should take self and the array index as paramaters and
return the values in that slot of the array

The set method should take self, an index, and the values as paramaters
and it should modify the real "C-version" of the array.

4) when you execute your python function, it will be given this
"wrapper" object as a paramater. It can then call into the methods in
the wrapper object to access and change the C-array "in-place" without
converting it at all. Each new value which is set into the array
is converted on the fly.

5) If you like, you can convert get() and set() to __get__() and __set__
so you can use array access syntax to access the array from python.


--
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Passing a 2D array as a pointer to a pointer PeterOut C Programming 3 08-28-2008 08:36 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Passing a pointer to pointer as a multi-dimensional array entitledX C Programming 4 01-12-2006 01:22 AM
Re: Passing pointer to array from C to Python..and modifying same array in python? J W Python 0 07-14-2003 02:59 PM



Advertisments