Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-03

constref question

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

José Mejuto

Hello FPC-Pascal,

The parameter passing mode "constref" ensures that something is passed as reference and never as value, so my question is using something like this:

type
  pmyrec=^myrec;
  myrec=record
    a: integer;
    b: integer;
    c: integer;
  end;
  MyInterf = interface
  [....]
    procedure test(const param: pmyrec); safecall;
  end;

Calling test is ok, but if I define test as:

  MyInterf = interface
  [....]
    procedure test(constref param: pmyrec); safecall;
  end;

it fails with a sigsegv. Are both expected to have the same behavior in this case ? (pointer to record).

Best regards, José
Jonas Maebe
to José Mejuto
José Mejuto wrote 9 lines
Calling test is ok, but if I define test as: MyInterf = interface [....] procedure test(constref param: pmyrec); safecall; end; it fails with a sigsegv. Are both expected to have the same behavior in this case ? (pointer to record).

"constref" is basically the same as "var" as far as passing the parameter is concerned. That means that in the case above, you are passing a pointer to a pointer to a record. If you just want a pointer to a record, use this:

procedure test(constref param: myrec); safecall;

Jonas

José Mejuto
to Jonas Maebe

Hello FPC-Pascal,

you wrote
> it fails with a sigsegv. Are both expected to have the same behavior > in this case ? (pointer to record).

JM> "constref" is basically the same as "var" as far as passing JM> the parameter is concerned. That means that in the case above, you JM> are passing a pointer to a pointer to a record. If you just want a JM> pointer to a record, use this:

Ok, that's 100% clear. I'm passing a pointer to a record based in the XPIDL description and sometimes is quite difficult to interpret how a parameter should be defined based in the IDL description as it is quite C centric instead platform agnostic.

When no modifier is applied (var, const,...) the behavior is deterministic or can change from platform to platform, or by ABI ?

Best regards, José
leledumbo
to José Mejuto
When no modifier is applied (var, const,...) the behavior is deterministic or can change from platform to platform, or by ABI ?

Deterministic. Pointer based values (Classes, Interfaces) are passed by reference. Others are by value. An exception to Ansi- and Wide- Strings, they're passed by reference but when a modification is done the copy on write semantics will make a copy thus simulating the pass by value. The one I'm not sure is Dynamic Arrays.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/constref-question-tp3410272p3412692.html Sent from the Free Pascal - General mailing list .
José Mejuto
to leledumbo

Hello FPC-Pascal,

you wrote
> When no modifier is applied (var, const,...) the behavior is > deterministic or can change from platform to platform, or by ABI ?

l> Deterministic. Pointer based values (Classes, Interfaces) are passed by l> reference. Others are by value. An exception to Ansi- and Wide- Strings, l> they're passed by reference but when a modification is done the copy on l> write semantics will make a copy thus simulating the pass by value. The one l> I'm not sure is Dynamic Arrays.

Great, thank you.

Best regards, José