I get an Access violation reading location when using mxCreateCh​arMatrixFr​omStrings

조회 수: 4 (최근 30일)
I am using mxCreateCharMatrixFromStrings in a C++ console app. I have read in the 'string' from a .mat file into a char *cData[10] using mxArrayToString(). I can see the string in cData[0] and it is what I expected. I take that variable and then call the following in C++:
mxArray *pMatr = nullptr;
pMatr = mxCreateCharaMatrixFromStrings(345, (const char**)cData[0]); <-- this line is where I get the access violation error!
my goal is to have an pointer to an 1X344 mwArray to pass into a matlab generated library.
I am using MatLab R2014 as the matlab generated library needed to be 32 bit.
Thanks,

채택된 답변

James Tursa
James Tursa 2018년 12월 11일
편집: James Tursa 2018년 12월 11일
You should show the entire code you are using, not just a snippet of it. Also, the fact that you have a typo in the function name (which would have caused a LINK error) tells me that you typed this code in manually instead of copying and pasting it. Please don't do that! Just copy and paste the actual code.
That being said, I assume you have done something like this:
mxArray *pMatr = nullptr;
char *cData[10];
cData[0] = mxArrayToString( something );
pMatr = mxCreateCharMatrixFromStrings(345, (const char**)cData[0]);
The variable name cData does in fact reduce to type (char **) when used in an expression, so it is already of the correct type to simply pass in directly as the 2nd argument. And in fact that is precisely what mxCreateCharMatrixFromStrings is expecting. Also, the 345 will cause a seg fault since there are only 10 physical elements in the cData array. So it needs to be something like this:
mxArray *pMatr = nullptr;
char *cData[344];
cData[0] = mxArrayToString( something );
// other code here to assign cData[1], cData[2], ..., cData[343]
pMatr = mxCreateCharMatrixFromStrings(344, cData);
Also note that in R2016b and earlier, the allocated memory from the mxArrayToString( ) call is not on the garbage collection list. So you would have to manually mxFree( ) each individual cData element to avoid memory leaks.
  댓글 수: 1
Edwardo Medina
Edwardo Medina 2018년 12월 11일
So by copying the whole code in I can guess that in trying to resolve the issue you are reproducing it locally from the submitted code. Good to know for the futrue.
thank you,

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

추가 답변 (1개)

Mohan Feng
Mohan Feng 2018년 12월 11일
It seems that in your code, cData[0] is of type char*, but you convert it into type char**, which may cause a segmentation fault.
  댓글 수: 2
Edwardo Medina
Edwardo Medina 2018년 12월 11일
I did find that the call mxCreateCharaMatrixFromStrings(345, (const char**)cData[0]); would work when I changed the number of rows from 345 to match the number of char* values in the cData array. I had incorrectly assumed that the call was only looking for one value to replicate through an array matching the number of rows I needed.
thanks,
James Tursa
James Tursa 2018년 12월 12일
I don't believe you. Passing in cData[0] as the 2nd argument couldn't possibly have worked, since that address points to a string of characters and is not the same as cData all by itself.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by