[Matrix-SIG] Freeing malloc'd array in Python

Scott M. Ransom ransom@cfa.harvard.edu
Mon, 07 Sep 1998 14:28:21 +0000


Konrad Hinsen wrote:

> Yet another way, if you don't care messing around with undocumented
> data structures, is creating a small Python array with its
> data space, freeing the data space, substituting a pointer to the
> data space you have, and patch the other entries in the array data
> structure to reflect the correct size.

I think I will play around with this.

> But unless you are working with really large arrays, just stick
> to the solution you have!

This is the problem.  I am often working with arrays that are approx. equal to
about half of the real memory size of my machine.  (Pulsar observations generate
_a lot_ of data!)  So any method that has me copying memory I would like to try
and avoid.

> I don't know if there are many C libraries out there that return
> allocated memory blocks to the caller (which I think is not a good
> approach for a variety of reasons). If there are, then NumPy should
> offer some way to handle freeing.

I agree.  I wrote the library myself and the reason why I have functions returning
memory blocks is that for many of the calls, the size of the block is unknown when
the block is requested.  So the routine calculates the size of the memory needed,
allocates it, works with it, and returns the memory block along with its size.
All I have to do is free it.

I could avoid this by breaking the code up into individual pieces that do each of
the above, but that makes the code much less readable, IMHO.

I don't know.  Now that I have "found" Python and NumPy (about a week ago -- and I
must say that I am quite amazed by it....) maybe I'll have to do some library
re-writing to make it a little more Python/NumPy/SWIG friendly.

Thanks for the help,


Scott

--
Scott M. Ransom
Phone:  (580) 536-7215             Address:  703 SW Chaucer Cir.
email:  ransom@cfa.harvard.edu               Lawton, OK  73505
PGP Fingerprint: D2 0E D0 10 CD 95 06 DA  EF 78 FE 2B CB 3A D3 53