What if you have a pointer to a record

What if you have a pointer to a record

Type
MyPointerToRecord = ^MyRecord;
MyRecord = Record
SomeField : Integer;
end;

and then in the code: you don't use the ^ to reference the field?

Var
LMyPointer : MyPointerToRecord;
begin
new(LMyPointer );

LMyPointer .SomeField := 6;

end;

The compiler doesn't complain (maybe it does give a warning, but we have thousands we need to clean up). And the code SEEMS to work. Any ideas? Does the compiler "fix" the code?


The "correct" code should be:

LMyPointer^.SomeField := 6;

Comments

  1. I started using Delphi with Delphi 3, and I somehow didn't stumble over this until last year...

    ReplyDelete
  2. Paul TOTH which has bitten me multiple times

    Actually, the same problem exists with dynamic arrays vs. static arrays:
    @SomeStaticArray points to the first entry
    @SomeDynamicArry points to pointer to the array
    Only way to be sure is @SomeArray[Low(SomeArray)].

    ReplyDelete
  3. Paul TOTH Yep, I've done that, and didn't get what I expected. :)

    ReplyDelete

Post a Comment