Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Calling anexternal C function that returns a pointer to a struct

Calling anexternal C function that returns a pointer to a struct

Hello,

I'm trying to call an external C function that returns a pointer to a struct.

Here is the program I use (in OpenModelica :

record rec
  Integer a;
  Integer b;
end rec;

function Chirp
  input Real id;
  input Real id2;
  output rec u;

  external u = Chirp(id, id2) annotation(Library = "Chirp");
end Chirp;

model ChirpSignal
  rec u;
  parameter Real id = 1;
  parameter Real id1 = 2;
equation
  u = Chirp(id, id1);
end ChirpSignal;


the programm in C is :

typedef struct {
int id1;
int id2;
}mystruct;


mystruct* Chirp(double a,double b)
{
mystruct* test;
test = malloc(sizeof(mystruct));
test->id1 = 2;
test->id2  =3;

return test;
}


The problem is that I don't have the right to change the C-program, I have to keep it like it is.
Can anybody please tell me if there is something to change in the OpenModelica programm so as to make it work.
Thank you.



Re: Calling anexternal C function that returns a pointer to a struct

help please !!!!

Re: Calling anexternal C function that returns a pointer to a struct

What is the error message?

Adeel.

Re: Calling anexternal C function that returns a pointer to a struct

Hi Adeel,

this is the error message I get :

"assert | debug | <p>ERROR: Too many event iterations. System is inconsistent. Simulation terminate.</p>"

I have to say that when I try to return a struct instead of a pointer to a struct, it works just fine. for exemple the following exemple works fine :

typedef struct {
int id1;
int id2;
}mystruct;

mystruct Chirp(double a,double b)
{
mystruct test;
//test = malloc(sizeof(mystruct));
test.id1 = 1;
test.id2 = 2;
return test;
}

do you have any idea or solution for my problem?
Thank you.

olaama

Re: Calling anexternal C function that returns a pointer to a struct

Hi adeel it's me again,

In the definition of "record rec" I changer the Integers by Real, and I don't have the error anymore, but the values I get for a and b are totally absurd. (Instead of having a=2 and b=3, I get a = 4.31109e-308, b=2.78583e+165.
Any Idea ?

Re: Calling anexternal C function that returns a pointer to a struct

Note that Modelica spec says structs are passed by reference. This means you cannot return them from a function, but need to pass the output as an argument to the function.

There are 0 guests and 0 other users also viewing this topic
You are here: