MEX compile type error

2 views (last 30 days)
Alex Coningham
Alex Coningham on 26 May 2014
Commented: Alex Coningham on 26 May 2014
Hi guys,
MATLAB Version 7.12.0.635 (R2011a) 32-bit Windows
I am working with some (very cool!) code for texture synthesis and have it working OK, though MATLAB is asking for a .c file to be compiled. I have researched the process, and after pointing MATLAB to the correct folder, I ran the "mex innerProd.c" command, and it threw two errors, first it was looking for "strings.h", which I fixed by manually changing that to "string.h" in the .c file, but there is a second error that I cannot work out. Below is the error and the code I think it is pointing to.
EDU>> mex innerProd.c
Error innerProd.c: 36 type error in argument 2 to `sprintf'; found `int' expected `pointer to const char'
1 errors, 0 warnings
C:\PROGRA~2\MATLAB\R2011A~1\BIN\MEX.PL: Error: Compile of 'innerProd.c' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
Here are lines 35-37 of innerprod.c
if (plhs[0] == NULL)
mexErrMsgTxt(sprintf("Error allocating %dx%d result matrix",wid,wid));
res = mxGetPr(plhs[0]);
Any help would be greatly appreciated.
Cheers, Alex
p.s. The texture synthesis package comes from this website

Accepted Answer

James Tursa
James Tursa on 26 May 2014
Edited: James Tursa on 26 May 2014
I don't know how this could have ever worked. In the first place, the arguments to sprintf are wrong. E.g. the 1st argument needs to point to a writable buffer to hold a printed result and you've got a constant character string there. 2nd argument is wrong also. In the second place, the function result of sprintf is an integer value giving the number of characters written, while mexErrMsgTxt is expecting a string argument. So I will give you one simple way to deal with this, and then point out that this code probably isn't even necessary in a mex routine.
You could change this:
if (plhs[0] == NULL)
mexErrMsgTxt(sprintf("Error allocating %dx%d result matrix",wid,wid));
To something like this:
if (plhs[0] == NULL) {
mexPrintf("Error allocating %dx%d result matrix",wid,wid);
mexErrMsgTxt("plhs[0] is NULL");
}
However, in a mex routine, if a memory allocation fails you don't drop down to the next line ... the mex routine simply terminates control back to the caller with an error. So if plhs[0] is the result of some mxCreateWatever call, and that call fails because of a memory allocation error, you WILL NOT drop down to the next line where you can test the plhs[0] value for NULL. So in all likelihood you do not need that if-test at all. If you got there, plhs[0] is guaranteed to not be NULL and that test will never be true (again assuming that it was the result of a mxCreateWhatever call).
  1 Comment
Alex Coningham
Alex Coningham on 26 May 2014
This solved my issue, and the file compiled without error, many thanks for your response.
And thank you for your explanation, after implementing your solution to resolve the issue I went back to understand why it was not working and indeed unnecessary.
Many thanks, Alex

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!