Matlab Coder : Problem with string

조회 수: 6 (최근 30일)
Michel
Michel 2011년 11월 25일
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
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
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.

Mike Hosea
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

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by