Matlab coder generated a c function which has void output, even though the matlab function has double array input
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Darshan Ramakant Bhat
le 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 :
https://www.mathworks.com/help/coder/ug/generate-and-modify-an-example-cc-main-function.html#buod5pb
Hope this will help you.
0 commentaires
Voir également
Catégories
En savoir plus sur MATLAB Coder dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!