Matlab coder generated a c function which has void output, even though the matlab function has double array input
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuestas (1)
Darshan Ramakant Bhat
el 24 de Mzo. de 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 :
https://www.mathworks.com/help/coder/ug/generate-and-modify-an-example-cc-main-function.html#buod5pb
Hope this will help you.
0 comentarios
Ver también
Categorías
Más información sobre MATLAB Coder en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!