2

I would like to use MATLAB Coder to generate an executable (or a function in an object file) that accepts a pointer to an array as an input.

I used libpointer to create a pointer object and then tried to compile with the following codegen command:

codegen -config:lib foo -args {coder.typeof(pointer_object_name)}

The resulting error message reported that coder.typeof does not support the lipointer type.

My ultimate goal is to create something that can be called from another C function, with no MATLAB in sight, and receive a pointer to an array as an input. Can MATLAB Coder generate something like that?


@ryan-livingston asked for the signature of the function I would like MATLAB Coder to generate.

Suppose that samples is a pointer to an array of floats. I think I want MATLAB Coder to create a void foo(float *samples) that performs various computations on those floats and perhaps writes results to a file or socket.

Now that I have the attention of @ryan-livingston, I suppose I should ask the following.

  • Can Coder make functions such as resample work with pointers?
  • Are pointers already being used under the hood, making my concern unnecessary?
3
  • 1
    Can you post an example of the precise C signature you're looking for? That will help inform the discussion. Commented Nov 13, 2020 at 14:27
  • @RyanLivingston: Thank you for responding. I have added a signature to my question. Commented Nov 15, 2020 at 19:20
  • Added some info. Can you also say more about your use case of why you'd like pointers to show up on interfaces or be used internally? What's the motivation: testing, efficiency, etc? Commented Nov 16, 2020 at 20:53

1 Answer 1

2

If you just generate code with a fixed-size array input, the generated code will be able to accept a pointer. For example:

function x = foo(x)
x = 2*x;
% You can use MATLAB fopen, fprintf, fwrite here to write x to a file

>> codegen foo -args zeros(10,20) -config:lib -report

produces the interface:

void foo(double x[200]);

which is the same as:

void foo(double *x);

because of array to pointer decay on calls in C.

Note that I've used the x = foo(x) syntax to have Coder pass x by reference to foo. Functions declared with the same variable as both input and output generally produce pass by reference when also called with the same variable as input and output at the callsite.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I thought of another situation in which I'd like to pass a C pointer to a function defined in an m-file: it would be convenient to pass argv directly to a function defined in an m-file. Is there a way to do that?

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.