Link to home
Start Free TrialLog in
Avatar of Aceelec
Aceelec

asked on

Is there a neat way to find the end of a record?

Hey experts, quick question... difficult to ask but.

Is there any way to define a field of a record that takes up no space.  To illustrate why I'd need this I'll give an example:

I have a TMyRecord, which may or may not have a packet of data (TPacket) at the end of it.

Currently the only way I know of finding the location of this TPacket is

var
  MyRecord : TMyRecord
begin
  Packet := TPacket(Pointer(Cardinal(@MyRecord)+SizeOf(TMyRecord))^)
end;

which works but is very messy.  Where as I could define TMyRecord as this:

TMyRecord = record
  field1, field2, field3, field4 : integer;
  endofrecord : boolean;
end;

and replace the code with this:

var
  MyRecord : TMyRecord
begin
  Packet:= TPacket(@MyRecord.endofrecord)
end;

Which is a lot neater, yet wastes the space of a boolean for myrecords that are not followed by TPackets.  (A boolean, in a non-packed record is 8 bits... which is a bit of a waste imo).  So is there any way to put a field in a record that takes no memory/has no value?
ASKER CERTIFIED SOLUTION
Avatar of Meldrachaun
Meldrachaun

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aceelec
Aceelec

ASKER

Only problem with that is this is used in the tightest of loops - function call overheads are quite noticeable (although it could inlined I guess...)

Ended up using the boolean approach, although only allocating the record it's size less 4 bytes (I mistakenly said 8 bits earlier, 32...). =) Awarding points :)
Slightly lighter syntax:

TMyRecordWithoutPacket = record
  field1, field2, field3, field4 : integer;
end;

TMyRecordWithPacket = record
  f: TMyRecordWithoutPacket;
  Packet: TPacket;
end;

var
  myrecord: TMyRecordWithoutPacket;
begin
  mypacket := TMyRecordWithPacket(myrecord).Packet;
...
use a byte variable instead of a boolean
Avatar of Aceelec

ASKER

I like it alkisg =) thanks!
You're welcome!

Alkis