0

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.

8
  • This looks like the retArray is not initialized properly, since it's size member points to 0 instead of an allocated int32_T. It would help, if you could provide us with your whole code. -- Edit: not your own code, but the generated :p Commented Dec 6, 2013 at 10:59
  • This could be tricky, because there is more than 100000 lines in this function. But you assume there could be a problem in code translated with Matlab Coder? Commented Dec 6, 2013 at 11:05
  • I just realized I am looking right at the error. Commented Dec 6, 2013 at 11:09
  • This up there in comment is basically it. There are just a few lines more to read and store user input from command line to variables, that are passed in to myFun. Commented Dec 6, 2013 at 11:12
  • 1
    Did you try to generate code from matlab-code actually calling the function? that would presumably show you how to properly initialize the array. Commented Dec 6, 2013 at 11:20

1 Answer 1

1

You call this from main:

struct emxArray_real_T *result = malloc(sizeof(struct emxArray_real_T));

// result->size == 0;
// You would need to initialize this too.
// Somewhere in myFun it is accessed like this:
//   *retArray->size = x;
// or
//   x = *retArray->size;
// which causes the fault.

myFun(input arguments, result);

Since I don't know anything about Matlab, I can't tell you if this is something you have to do, or if this should be done by the generated code somewhere.

You can try to fix it like this:

result->size = malloc(sizeof(int32_T));
Sign up to request clarification or add additional context in comments.

4 Comments

do not cast the result of malloc. This will hide compiler errors.
This works now, great. I put result->size=malloc(sizeof(int32_T)) in my main function, before calling myFun. @hhachem pointed out that this hides compiler errors, so is this something a shoul be careful about or shoul i do this some other way? Again thanks for you answer Qntm.
There are multiple reasons not to cast malloc's return value. 1) It is a void* which gets implicitly casted to any other pointer type. Hence the additional cast is unnecessary verbose and makes reading harder. 2) The cast hides an compiler error if you forget to include <stdlib.h>. There are a bunch of good articles about this problem here at SO.
Generally you should of course check malloc's return value against 0, since this is the return value indicating allocation errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.