Opened 5 years ago

Last modified 3 years ago

#6055 new defect

Bug when passing Boolean arrays as external function arguments

Reported by: Bernhard Thiele Owned by: Andreas Heuermann
Priority: normal Milestone:
Component: Run-time Version: v1.16.0-dev
Keywords: Cc:

Description

In the model below a Boolean array is passed to an external function. The returned Boolean array should be identical. This works as expected when using the Cpp run-time, but fails for the default C run-time.

model ExternalArrayBug
  "Shows an OMC C run-time bug when passing Boolean arrays"
  output Boolean u[4](each start=false,each fixed=true);
  output Boolean y[4](each start=false,each fixed=true);

protected
function booleanIdentity
  extends Modelica.Icons.Function;
  input Boolean u[:];
  output Boolean y[size(u,1)];
  external "C" boolean_identity(u, size(u,1), y, size(y,1))
  annotation(Include = "
  #include \"ModelicaUtilities.h\"
  void boolean_identity(const int *u, size_t u_n, int *y, size_t y_n) {
    for (int i=0; i<u_n; ++i) {
      ModelicaFormatMessage(\"u[%d]=%d\\n\", i, u[i]);
      y[i] = u[i] == 0 ? 0 : 1;
    }
  }
  ");
end booleanIdentity;

equation
  when sample(0,0.1) then
    u = not pre(u);
    y = booleanIdentity(u);
  end when;
end ExternalArrayBug;

Below is part of the ModelicaFormatMessage output which I get running OpenModelica 1.16.0~dev-541-gfd2d61b using Linux.

u[0]=16843009
u[1]=0
u[2]=0
u[3]=0
u[0]=0
u[1]=1
u[2]=0
u[3]=0
u[0]=16843009
u[1]=0
u[2]=0
u[3]=0
u[0]=0
u[1]=1
u[2]=0
u[3]=0

Change History (3)

comment:1 by Francesco Casella, 5 years ago

Milestone: Future1.17.0

It is perhaps interesting to note that 16843009 = 224 + 216 + 28 + 20

comment:2 by Francesco Casella, 4 years ago

Milestone: 1.17.01.18.0

Retargeted to 1.18.0 because of 1.17.0 timed release.

comment:3 by Francesco Casella, 3 years ago

Milestone: 1.18.0

Ticket retargeted after milestone closed

Note: See TracTickets for help on using tickets.