MATLAB returns empty string from C-MEX file

I have an external C function which takes a double pointer and returns a char. The problem is that when I interface that function, I cannot extract the character. Always an empty string is returned by MATLAB. What is the problem? The mex compiler returns no errors and warnings.
Here is a minimum working example:
#include "mex.h"
char mwe(double* a)
{
return 'h';
}
/* Gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char *code;
double *input;
input = mxGetPr(prhs[0]);
*code = mwe(input);
plhs[0] = mxCreateString(code);
}

 Respuesta aceptada

James Tursa
James Tursa el 19 de Nov. de 2018
Editada: James Tursa el 20 de Nov. de 2018
It is hard to believe that this code did not crash MATLAB. The issues:
char *code; <-- This is an uninitialized pointer ... it contains a garbage address
double *input;
input = mxGetPr(prhs[0]);
*code = mwe(input); <-- this copies a char into the garbage address (should have crashed MATLAB here)
plhs[0] = mxCreateString(code); <-- Attempts to create a MATLAB char array from the bytes at that garbage address
That mxCreateString( ) call could easily have crashed MATLAB as well. If you want to copy characters into a pointer, then you need to allocate that pointer first. E.g.,
code = mxMalloc(2);
:
code[0] = mwe(input);
code[1] = '\0'; /* Null terminate the string */
:
mxFree(code);
It is not really clear what your ultimate intent is passing a double pointer into mwe and getting a char passed back, so I can't give you any advice there unless you give more details.

5 comentarios

Zoltán Csáti
Zoltán Csáti el 19 de Nov. de 2018
Thanks for the quick reply.
"It is not really clear what your ultimate intent is passing a double pointer into mwe and getting a char passed back" This was just a minimal example. In the original function (not written by me), there is a whole bunch of calculation. What is given is that it returns a char type. I want to catch that char type by creating a mex gateway to the existing C code. So I do not want to modify the original code.
Zoltán Csáti
Zoltán Csáti el 19 de Nov. de 2018
Your solution works perfectly. Thank you.
Just for curiosity: where is it documented that MATLAB requires a null-terminated string as output from C?
James Tursa
James Tursa el 19 de Nov. de 2018
Editada: James Tursa el 19 de Nov. de 2018
Most C functions that have a (char *) type as an input argument are expecting a Null terminated string. This is true for C in general, not just MATLAB C/C++ API functions. So anytime you see that as an input agrument you should be expecting to supply a Null terminated string unless there is specific documentation otherwise. The terminology "C-style string" or simply the single word "string" in C means "Null-terminated string."
In the mxCreateString( ) doc they use the word "string" when describing the input argument str. Although not explicitly stated, just the use of the word "string" in a C context means "Null-terminated string."
Contrast that with the doc for mxGetString( ). Here, the description for the argument variable str simply calls it a "starting location." I.e., on input you are not expected to supply a C-style Null-terminated string ... you are simply supplying a memory address. The mxGetString( ) function will actually write charaters into this memory and Null-terminate it (i.e., it will create a C-style Null-terminated string at that memory location as an output of the function).
If you are always only getting a single char back and you want to create an mxArray string from it, I would just avoid that mxMalloc( ) and mxFree( ) stuff entirely. E.g., just do this instead:
char code[2] = {0,0}; /* Includes the null termination */
double *input;
input = mxGetPr(prhs[0]);
code[0] = mwe(input);
plhs[0] = mxCreateString(code);
Zoltán Csáti
Zoltán Csáti el 22 de Nov. de 2018
Yes, the C function I must interface always returns one char. Thanks for the suggestion.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre C Shared Library Integration en Centro de ayuda y File Exchange.

Productos

Versión

R2015b

Etiquetas

Preguntada:

el 19 de Nov. de 2018

Comentada:

el 22 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by