[PYTHON MATRIX-SIG] A related idea
Tue, 7 Nov 95 09:56:39 -0500
> Suppose we have the matrix class working.
What do you mean suppose ;) ?
> Suppose there is a Fortran
> or C array in the program which you would like to "adopt" as a
> matrix in Python. The only difference between this and a matrix created a
> normal way is that you have to remember never to free the memory.
> So it seems you might be able to do this if you had a method
> of creation (from C) which took a pointer,size,type kind of
> description. You could then increment the reference counter one extra, so
> that the memory is never freed, or a "don't free" flag could be in the
I agree that there should be a function to create a new matrix object from
an existing array in C (or FORTRAN). What we disagree about is the
semantics. I played around a while with the idea of "sharing" an array
between python and C. I found that as a general rule I ran into memory leak
problems (or worse). Using your scheme, python is never responsible for
freeing the allocated memory, this means that the C code must be responsible
for it, but the C code can't know when it is safe to free the memory
because even when it's done with it there might still be a python reference
to the memory.
Let me know if I'm completely missing something here, and this memory leak
issue isn't really a problem.
I made the general decision to completely avoid these sorts of issues by
(almost) never returning arrays that are references to other arrays. I
found that doing a malloc and a memcpy is usually sufficiently faster than
any operation I'm going to perform on the array that it could be ignored.
In order for this to be a valid assumption, I need to be working on a system
where I have "more than enough" memory hanging around. This is always true
for me, but I'm interested in cases where it's not.
Note: the following will be completely meaningless to you if you haven't
played with the alpha matrix object, so please ignore.
I'd add the following function to do what you're suggesting, but using my
semantics (this is off the top of my head, so there're no guarantees it will
PyObject *PyMatrix_FromDimsAndData(int nd, int *dimensions, char type, void
PyMatrixObject *ret = PyMatrix_FromDims(nd, dimensions, type);
memcpy(ret->data, data, NBYTES(ret));
return (PyObject *)ret;
I'll add something like this to the next release.
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: firstname.lastname@example.org
administrivia to: email@example.com