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(TMy Record))^)
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.endofrec ord)
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?
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(@
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.endofrec
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Slightly lighter syntax:
TMyRecordWithoutPacket = record
field1, field2, field3, field4 : integer;
end;
TMyRecordWithPacket = record
f: TMyRecordWithoutPacket;
Packet: TPacket;
end;
var
myrecord: TMyRecordWithoutPacket;
begin
mypacket := TMyRecordWithPacket(myreco rd).Packet ;
...
TMyRecordWithoutPacket = record
field1, field2, field3, field4 : integer;
end;
TMyRecordWithPacket = record
f: TMyRecordWithoutPacket;
Packet: TPacket;
end;
var
myrecord: TMyRecordWithoutPacket;
begin
mypacket := TMyRecordWithPacket(myreco
...
use a byte variable instead of a boolean
ASKER
I like it alkisg =) thanks!
You're welcome!
Alkis
Alkis
ASKER
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 :)