Matlab coder generated a c function which has void output, even though the matlab function has double array input

13 views (last 30 days)
My matlab function is of the following form =
function Y = fn(X)
%some code here
end
where both X and Y are vectors of type double
I used Matlab coder to convert this into a c program function.
The c file is of the form -
void fn(X,Y){
//stuff goes here
}
But I want the function to return Y. (I assume in this format, I have to send the pointer of output as the input argument).
How to I change this?

Answers (1)

Darshan Ramakant Bhat
Darshan Ramakant Bhat on 24 Mar 2021
This is an intentional design in the generated code and the users dont have control over it to change this behaviour as of today.
Reason for the design :
In the current design, the caller function will need to create the pointer (memory) and owns it. Memory creating and the deletiong is done in the same function.
int main {
// Allocate input and output memory
int *input = (int*)(calloc(10,sizeof(int)));
int *output = (int*)calloc(10,sizeof(int)));
for (i =0;i<10;++i) {
// Fill the input
input[i] = i;
}
// Use the generated code
foo(input,output);
// Do something
//Destroy the memory
free(input);
free(output);
}
If you return the pointer as output, that pointer needs to be created inside the function, so the ownership of the memory is not clear.
int* foo(int *input) {
// does something
// Allocates the memory
int *out = (int *)(calloc(numel,sizeof(int)); // Who owns this memory ? this may be leaked.
return out; // Also the size information is lost due to array decaying into pointer.
}
The generated code contains an example main.c file which shows a sample use of the generated function. You can take a look at it :
Hope this will help you.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!