[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem with mexPutVariable
From: |
Bård Skaflestad |
Subject: |
Re: Problem with mexPutVariable |
Date: |
Wed, 22 May 2013 20:08:00 +0200 |
On Wed, 2013-05-22 at 16:18 +0000, DONATI, KORY D CTR USAF AFSPC
WROCI/InDyne wrote:
> I asked a similar question previously that went unanswered. I have dug
> into my problem more and have found what I think to be the root cause:
> mexPutVariable. I am still new to using mex files and their language,
> so I might not have my code correct.
It's been a while since I did any MEX coding so I'm hazy on the details,
but I do believe there is at least one misunderstanding in the code you
posted. It is unrelated to mexPutVariable().
Specifically, the statements
> double arr1[5];
/* ... */
> /* Create an mxArray. */
> array_ptr = mxCreateDoubleMatrix(1, 5, mxREAL);
>
> /*Set array to the mxarray*/
> mxSetPr(array_ptr, arr1);
don't do what you probably think they do. At least not in the
commercial product from TMW. In particular, the mxSetPr() function call
is incorrect. One uses function mxSetPr() to *replace* an existing
mxArray's entire (real) data array. This, among other things, means
that the array passed as the second parameter to mxSetPr() *must* be
dynamically allocated--often through mxMalloc() or similar. Otherwise
you have undefined behaviour once the (stack-allocated) 'arr1' goes out
of scope. You also get a memory leak because mxSetPr() does not free
the memory of the data array it replaces.
I *think* you probably want to do something like this instead
/* Create and fill 'arr1' as before */
/* Create an mxArray */
array_ptr = mxCreateDoubleMatrix(1, 5, mxREAL);
/* Initialise the mxArray data values */
memcpy(mxGetPr(array_ptr), arr1, sizeof arr1);
but whether or not this improves the overall situation with respect to
mexPutVariable() I have no idea.
By the way, this discussion is very nearly off-topic as far as I can
tell.
Sincerely
--
Bård Skaflestad <address@hidden>