I have a next code:
type THead = packed record
znmpc: byte;
znmpcch: array [0..1] of char;
znc, zneispr, zkpd, zkps, nd: byte;
zb9, zb10, zb11, zb12, zb13, zb14, zb15: byte;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
db: ^THead;
a: array [0..9] of byte;
begin
a[7] := 9;
db := @a;
ShowMessage(IntToStr(db.nd));
end;
Is this code safe? I’m worried about next thing: size of struct more than size of buffer, and i have a fear about it. The value of members of struct after nd has no meaning for me. I want to know can this code throw an exception in certain circumstances, and if so in what?
I’d guess it is perfectly safe if you handle it carefully. But you need to be certain that you do not forget that you may not access
zb11..zb15. In addition, recall that acharis 1 byte prior to Delphi 2009, and 2 bytes in Delphi 2009 and later versions. Also, maybe it is worth to make the recordpacked(don’t think you need that in this case, but it is never wrong to be pedantically explicit)? Finally, of course you have to be careful so that the array doesn’t go out of scope!