Opened 14 years ago

Last modified 14 years ago

#1262 closed defect (fixed)

Modelica-function's local variables do not get initialized!

Reported by: asodja Owned by: asodja
Priority: high Milestone:
Component: Code Generation Version: 1.6.0
Keywords: Cc: asodja, Per Östlund

Description

Local variables of functions are not initialized in the generated code.

Consider the following example:
{{{function testFcn

output Real res;

protected

Real coef = -42.0;

algorithm

res := coef;

end testFcn;}}}
The generated code for the function is:
{{{fcnLocalValInit_testFcn_rettype _fcnLocalValInit_testFcn()
{

fcnLocalValInit_testFcn_rettype tmp1;
state tmp2;
modelica_real res;
modelica_real coef;
tmp2 = get_memory_state();

res = coef;

_return:
tmp1.targ1 = res;
restore_memory_state(tmp2);
return tmp1;

}
}}}
As it can be seen, the variable coef is never initialized, so function returns arbitrary value!

Initialization of local variables are also used in Modelica Standard Library,
for example, in {{Modelica.Blocks.Sources.TimeTable.getInterpolationCoefficients}}!

Change History (3)

comment:1 by Per Östlund, 14 years ago

Fixed in revision 6294, see test case mosfiles/LocalVariableInit. The fix also enables default values on output variables as a side-effect, since we treat output variables and local variables in the same way.

comment:2 by Martin Sjölund, 14 years ago

I guess that's fine; the standard says outputs should not be initialized by default if I recall correctly, but I guess initializing won't hurt much.

comment:3 by Per Östlund, 14 years ago

The specification says (section 12.2): "A formal parameter or local variable may be initialized through an assignment (:=) of a default value in its
declaration.". And section 12.1 also shows examples of this. So my interpretation of the specification is that output parameters should be initialized if it has a default value.

Note: See TracTickets for help on using tickets.