Handling Pointers in C++ library

1 view (last 30 days)
David Gault
David Gault on 20 May 2011
Commented: Krishna on 30 Sep 2013
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 Comments
James Tursa
James Tursa on 20 May 2011
I assume you meant char * above, not char.
Krishna
Krishna on 30 Sep 2013
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.

Sign in to comment.

Answers (2)

Titus Edelhofer
Titus Edelhofer on 20 May 2011
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 Comments
David Gault
David Gault on 20 May 2011
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 on 20 May 2011
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).

Sign in to comment.


James Tursa
James Tursa on 20 May 2011
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.

Community Treasure Hunt

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

Start Hunting!