필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

passing varibales to mex

조회 수: 1 (최근 30일)
PT
PT 2019년 1월 8일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi,
I have a question regarding MEX memory allocation. I am novice to writing mex files, so I apologize in advance for a very fundamental question.
I have a code which looks something like this -->
double *x
x = mxGetPr(prhs[0]);
blahblah(x) ; // changes value of x
plhs[0] = mxCreateNumericArray(1,dim,mxDOUBLE_CLASS, mxREAL); // dim is a const size_t
mxSetPr(plhs[0],x);
(I have Matlab17b so I am not using mxSetDoubles)
Does this piece of code make a copy of the variable x or simply assigns the pointer of x into plhs? In my opinion the use of mxCreateNumericArray will make a copy of x. Is my understanding correct?
Also, what is the best way to pass a variable to my mex function, change it, and return it back to Matlab without making a copy in c++.
Thank you
-PT
  댓글 수: 1
OCDER
OCDER 2019년 1월 8일

답변 (1개)

Jan
Jan 2019년 1월 8일
편집: James Tursa 2019년 1월 8일
mxCreateNumericArray creates a new variable. Overwriting the pointer to the reserved memory by mxSetPr will leak the memory and sharing the memory of the input and output will confuse Matlab's memory manager remarkably: It will crash.
To duplicate a variable use mxDuplicateArray. If you are a newcomer, avoid smart inplace techniques at first. They are tricky, not documented and prone to hard errors in the old R2017b API.

이 질문은 마감되었습니다.

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by