Handling Pointers in C++ library

Hi, I am trying to write a c++ library for Matlab. There are 2 functions that Im having issues with. I am writing a wrapper for image acquisition and have 2 SDK functions
QueueBuffer(int handle, char* buffer, int size) and WaitBuffer(int handle, char** buffer, int size, int timeout)
I can create a libpointer in MatLab and pass it in, is there a way I can keep the pointer and pass them through to the SDK without having to copy the data or reallocate the space?
I can get it working with use of the following but Id rather not have this extra ineffiency and Im sure there is a better way to handle it: int yLength = mxGetN(prhs[2])+1; char TheString; TheString = (char)mxCalloc(yLength, sizeof(char)); mxGetString(prhs[2],TheString,yLength);
Any help is much appreciated.

댓글 수: 2

James Tursa
James Tursa 2011년 5월 20일
I assume you meant char * above, not char.
Krishna
Krishna 2013년 9월 30일
Hi, I am trying to pass an image pointer from MEX file to another C-file to threshold the image in the C-file. The C-file has its own image type structure defined. Is it the problem of the header files or the library files as I have read somewhere? I can compile the code from matlab and can read the image. But the program breaks when I call the threshold function. Can I get some valuable suggestions. Thanks in advance.

댓글을 달려면 로그인하십시오.

답변 (2개)

Titus Edelhofer
Titus Edelhofer 2011년 5월 20일

0 개 추천

Hi,
hm, not sure I understand the question. For the second half: is the function mxArrayToString what you are looking for?
For the first half: how do you interact with your library? loadlibrary? Or mex files?
Titus

댓글 수: 2

David Gault
David Gault 2011년 5월 20일
Hi Titus,
Thanks for taking a look at this. Im interacting with the library through a mex file.
As for the second half, ideally id like to retain the pointer and pass it straight through as a parameter without having to copy the data. For example if i had the following code in my mex file:
else if(!strcmp(func,"AT_QueueBuffer")) {
unsigned int ui_handle = (unsigned int)*mxGetPr(prhs[1]);
int i_size = (int)*mxGetPr(prhs[3]);
AT_U8* buffer = // libpointer in parameter 2, ideally without having to reallocate memory for it or copy data //
Ret = AT_QueueBuffer(ui_handle,buffer,i_size); //SDK call
}
I hope that makes sense
With Thanks,
David
Kaustubha Govind
Kaustubha Govind 2011년 5월 20일
What type is AT_U8? Typically, you should just be able to use mxGetPr (for double*) or mxGetData (other types) to get the data in any mxArray and pass it on to other functions (be careful however, that the other library does not attempt to de-allocate the memory).

댓글을 달려면 로그인하십시오.

James Tursa
James Tursa 2011년 5월 20일

0 개 추천

MATLAB stores character data as 2-bytes per character. You can get a pointer to the character data with mxGetData, and pass that pointer down to your routine, but your routine will have to be able to handle the data as interleaved characters with a specified length (mxGetNumberOfElements) and will NOT be able to use it as a regular C-style NULL terminated string, which it isn't. If your routine must have a C-style NULL terminated string as input, then you are forced to copy the data ala mxArrayToString etc.

카테고리

도움말 센터File Exchange에서 Call C++ from MATLAB에 대해 자세히 알아보기

질문:

2011년 5월 20일

댓글:

2013년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by