Prev: extension g
Next: DLL Header
From: Paul on 7 Aug 2010 15:02 I have a C library that dynamically allocates some arrays. One function is called by a mex function and I wish to pass these double arrays to Matlab without duplicating the arrays. I currently do plhs[p] = mxCreateNumericMatrix (1, 1, mxDOUBLE_CLASS, mxREAL); mxSetData (plhs[p], (void *)my_array); mxSetM (plhs[p], (mwSize)my_nrows); This works but I get warnings: warning: mxFree: skipping memory not allocated by mxMalloc, mxCalloc, or mxRealloc Is there a cleaner way to do this? Due to the nature of the C functions they cannot accept preallocated arrays from Matlab nor can they use mxAlloc etc. Thanx for any hings. Paul
From: James Tursa on 7 Aug 2010 15:58 "Paul " <wessel(a)soest.hawaii.edu> wrote in message <i3kajb$meb$1(a)fred.mathworks.com>... > I have a C library that dynamically allocates some arrays. One function is called by a mex function and I wish to pass these double arrays to Matlab without duplicating the arrays. I currently do > > plhs[p] = mxCreateNumericMatrix (1, 1, mxDOUBLE_CLASS, mxREAL); > mxSetData (plhs[p], (void *)my_array); > mxSetM (plhs[p], (mwSize)my_nrows); > > This works but I get warnings: > > warning: mxFree: skipping memory not allocated by mxMalloc, mxCalloc, or mxRealloc > > Is there a cleaner way to do this? Due to the nature of the C functions they cannot accept preallocated arrays from Matlab nor can they use mxAlloc etc. > > Thanx for any hings. > Paul There is only *one* way to do this, and that is to *copy* the data from your C allocated memory block to a memory block allocated by one of the MATLAB API functions. You are trying to avoid this extra copy, but you can't. If you persist in trying to avoid this you will eventually crash MATLAB. The only solution that does not involve the extra copy is to first allocate the memory with one of the MATLAB API functions, then pass a pointer to that memory to the function that you want to alter or fill in the memory. If the function does not support that, then you are out of luck and must copy. You *cannot* mix C allocated memory (for lack of a better term) into an mxArray. James Tursa
From: Paul on 7 Aug 2010 16:25 Excellent answer; many thanks. Paul
|
Pages: 1 Prev: extension g Next: DLL Header |