Matlab Coder : Problem with string
10 次查看(过去 30 天)
显示 更早的评论
Hello,
I try to put Trace on my model
I call the tracing function of an external library.
In my model : I do
T_Trace('This is my trace');
In the T_Trace function I do :
function T_Trace( Texte ) %#codegen
if isempty(coder.target)
% Executing in MATLAB, call MATLAB equivalent of
% C function foo
calllib('MATLAB_TOOLS','Tracing',Texte);
else
% Executing in Embedded MATLAB, call C function foo
%
coder.ceval('Tracing',Texte);
end
end
When I generate a C++ code, Matlab coder create :
real_T tracing_c(real_T A) {
static const char_T Texte[16] = { 'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'm',
'y', ' ', 't', 'r', 'a', 'c', 'e' };
char_T b_Texte[16];
for (i0 = 0; i0 < 16; i0++) {
b_Texte[i0] = Texte[i0];
}
Tracing(b_Texte);
...
Why Matlab Coder create a "for" for make a copy of Texte into b_Texte ? Why Matlab coder make a copy ? The problem is when i make a lot of trace, i have a lot of "for" and i can't compil the C code because the number of for is too high ... How can i ask to matlab coder to not make a copy of all string ??
Thank you !
1 个评论
Kaustubha Govind
2011-11-28
Michel: I'm not sure if there is a way to avoid this type of generated code. I would recommend reporting this to MathWorks Tech Support.
回答(2 个)
Walter Roberson
2011-11-28
Coder must make a copy of the string if it cannot prove that the called function does not modify the string. Usually compilers are permitted (by the language standards) to write strings in to read-only-memory, and Coder does not know whether the eventual target compiler will have that property or not.
0 个评论
Mike Hosea
2011-11-28
You are passing texte as a read+write argument. If your function does not modify the argument, try coder.rref():
coder.ceval('Tracing',coder.rref(Texte));
Also, be careful when passing a string from MATLAB to C. In C strings are null-terminated, but in MATLAB strings are not null-terminated (their length is implicitly carried around with them). -- Mike
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!