Sign in Join

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

Mailing lists / fpc-pascal / 2022-10

Iterating over elements from GetDynArrayProp

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Hairy Pixels

How can I iterate over the pointer that is returned from GetDynArrayProp? I tried to cast it as a dynamic array of the element type but calling Length() on the returned value gave garbled data.

For example with a property of the type “array of TObject":

type
  TObjectArray = array of TObject;
  PObjectArray = ^TObjectArray;
var
  ObjectArray: PObjectArray;
begin
  ObjectArray := PObjectArray(GetDynArrayProp(AObject, PropertyInfo));
  writeln(Length(ObjectArray^));
end;

Regards,
Ryan Joseph

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 11 lines
How can I iterate over the pointer that is returned from GetDynArrayProp? I tried to cast it as a dynamic array of the element type but calling Length() on the returned value gave garbled data. For example with a property of the type “array of TObject": type TObjectArray = array of TObject; PObjectArray = ^TObjectArray; var ObjectArray: PObjectArray; begin ObjectArray := PObjectArray(GetDynArrayProp(AObject, PropertyInfo));

as far as I kbow, this should be

  ObjectArray := TObjectArray(GetDynArrayProp(AObject, PropertyInfo));

i.e. you get the actual array as a result.

Michael.

Hairy Pixels
to Michael Van Canneyt
Show quoted text 6 lines
On Oct 15, 2022, at 3:03 PM, Michael Van Canneyt wrote: as far as I kbow, this should be ObjectArray := TObjectArray(GetDynArrayProp(AObject, PropertyInfo)); i.e. you get the actual array as a result.

You’re right, I didn’t need that pointer cast. I’m still curious though why that didn’t work. Shouldn’t the pointer still just point to the dynamic array address and basically do nothing?

Regards,
Ryan Joseph

Michael Van Canneyt
to Hairy Pixels
Hairy Pixels wrote 10 lines
> On Oct 15, 2022, at 3:03 PM, Michael Van Canneyt wrote: > > as far as I kbow, this should be > ObjectArray := TObjectArray(GetDynArrayProp(AObject, PropertyInfo)); > i.e. you get the actual array as a result. > You’re right, I didn’t need that pointer cast. I’m still curious though why that didn’t work. Shouldn’t the pointer still just point to the dynamic array address and basically do nothing?

No, it's perfectly logical what is happening.
You can't cast a type (TA) to a pointer to that type (^PTA) and expect the compiler to automagically forget the extra dereference when you dereference it...

Michael.