Einzelnen Beitrag anzeigen

Benutzerbild von dummzeuch
dummzeuch

Registriert seit: 11. Aug 2012
Ort: Essen
1.604 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#14

AW: Delphi 12 und SendMessage

  Alt 20. Feb 2024, 09:37
You are right, but for best practice i prefer never to use variable, to avoid situations like the following
Code:
type
  PMyRecord = ^TMyRecord;

  TMyRecord = record
    Buffer: array[0..31] of Byte;
  end;

  TMyClass = class
  public
    Rec1: TMyRecord;
    Rec2: PMyRecord;
  end;
var
  C:TMyClass;
begin
  Writeln(IntToStr(SizeOf(C.Rec1)));  // 32 
  Writeln(IntToStr(SizeOf(C.Rec2)));  // 4 on 32bit, 8 on 64bit
  Readln;
end.
This thing will be dangerous or more impactful when TMyRecord is small like 4 or 8 and pass on one platform to fail on the other, can happen when updating legacy code or just redesigning data structures.
Unfortunately that opens the possibility for a different kind of error:
You check the size of the type you are using at some time and later on change the variable declaration to a different type. SizeOf(VariableName) will then automatically reflect that change while SizOf(TOldType) will still return the size of the original type. If you don't catch that, your code will be wrong. That's the reason I prefer using the variable instead of the type.
Thomas Mueller
  Mit Zitat antworten Zitat