I am translating some Matlab functions to C with Matlab Coder. Everything works to the point, where i want to return an array(transposed vector) from my function.
In Matlab I have a function:
function returnedArray = myFun(input arguments)
<function code>
This call to the function in C gets translatet to:
void myFun(input arguments, emxArray_realT *returnedArray)
<function code>
emxArray_real_T is a struct, that was generated by Matlab Coder:
struct emxArray_real_T
{
real_T *data;
int32_T *size;
int32_T allocatedSize;
int32_T numDimensions;
boolean_T canFreeData;
};
And real_T, int32_T... are created generic type definitions:
typedef double real_T;
I call this myFun from main:
struct emxArray_real_T *result = malloc(sizeof(struct emxArray_real_T));
myFun(input arguments, result);
When i run this, i get an error saying: Segmentation fault. gdb gave me this:
Program received signal SIGSEGV, Segmentation fault at:
99643: i0=retArray->size[0];
p retArray
$1 = (emxArray_real_T *) 0xc1d010
p retArray.size
$2 = (int32_T *) 0x0
p retArray.size[0]
Cannot access memora ar adress 0x0
Am i doing something wrong in my main function? I hope so, because translated code from Matlab in C is a mess, or maybe just for me as a novice. The code works fine, if I translate and compile it in C without the return value.
retArrayis not initialized properly, since it'ssizemember points to 0 instead of an allocatedint32_T. It would help, if you could provide us with your whole code. -- Edit: not your own code, but the generated :p