Hi, I read a file with array data (one per line), and send the arrays to a module in C. In the C module, I need to store pointers to the arrays, so I don't have to make a copy for each array it receives from python. I found that if I reuse the same variable name to create the array, the pointer's values in C program will be lost next time the variable is reassigned. For example: - for line in file: - myarray=create an int array from line - my_c_module.function(myarray) 'myarray' is reused to create a new array for each line, however I see that the data in myarray in previous loops (which is stored in C program) is destroyed. To avoid this, I create a python list that appends each 'myarray' instances, and thus are not overwritten. Is there a better approach to avoid this? Thanks in advance! _______________________________________________ Join Excite! - http://www.excite.com The most personalized portal on the Web!
I believe in your current setup there is no better way. But you should
seriously consider changing the way of using array data. Storing bare
pointers in the C side and not holding a reference to the object
providing the data in the C side is really error prone.
On 6/3/08, Jose Martin
Hi, I read a file with array data (one per line), and send the arrays to a module in C. In the C module, I need to store pointers to the arrays, so I don't have to make a copy for each array it receives from python.
I found that if I reuse the same variable name to create the array, the pointer's values in C program will be lost next time the variable is reassigned.
For example: - for line in file: - myarray=create an int array from line - my_c_module.function(myarray)
'myarray' is reused to create a new array for each line, however I see that the data in myarray in previous loops (which is stored in C program) is destroyed.
To avoid this, I create a python list that appends each 'myarray' instances, and thus are not overwritten. Is there a better approach to avoid this?
Thanks in advance!
_______________________________________________ Join Excite! - http://www.excite.com The most personalized portal on the Web!
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594
Lisandro Dalcin wrote:
I believe in your current setup there is no better way. But you should seriously consider changing the way of using array data. Storing bare pointers in the C side and not holding a reference to the object providing the data in the C side is really error prone.
exactly. as you are passing a ndarray into your C code, another option is that you could increase the reference count to that array in your C code (Py_INCREF). Then it would not be destroyed on the Python side. However, you'd have to make sure you Py_DECREF it when you don't need it anymore. There may be a way to tell the array that is does not "own" the data block also, but I don't know the numpy API enough to tell you how to do that. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (3)
-
Christopher Barker
-
Jose Martin
-
Lisandro Dalcin