[fpc-pascal] Some features I use daily in FPC I hope never go away!
Marc Santhoff
M.Santhoff at t-online.de
Thu Feb 22 20:33:33 CET 2007
Hi,
Am Donnerstag, den 22.02.2007, 13:18 -0500 schrieb Jason P Sage:
> I Hope the following never goes away - I use this stuff all the time!
>
> Types
> -----
> Cardinal, Pointer, Integer
> Always evaluate to the endian in use.
> SizeOf(Cardinal)=SizeOf(Pointer)=SizeOf(Integer)
>
>
> Offsets
> -------
> If I have a record or a class - and dereference the pointer "nil" I can
> always get the actual offset for a data element! I LOVE THIS.
>
> Example:
> ---------------CODE BEGIN
>
> Type rtMyRecord = record
> saMyName: ansistring;
> saMyEmail: ansistring;
> end;
>
> var rMyRecord:=rtMyRecord;
> lpMyRecord: pointer;
> uMyCardinalOffset: cardinal;
> begin
> lpMyRecord:=getmem(SizeOf(rtMyRecord));
> uMyCardinalOffset:=cardinal(@rMyRecord(nil^).saMyEmail);
> end;
>
> ---------------CODE END
>
> You can do the same thing with a class, without the carat "^" symbol. This
> is not such a big deal for many people because you can access the record or
> class elements like this:
> rMyRecord(lpMyRecord^).saMyEmail:='somwhere at somehow';
Since you're fiddling with low level stuff and I had a problem similar a
while ago:
Do you see a way of calculating the offset using types instead of
variables? Using your example I would like to use something like:
uMyCardinalOffset:=cardinal(rtMyRecord.saMyEmail);
This would be much cleaner with respect to reading the resulting code.
The HDF5 library makes heavy use of calculating offsets of record
members.
Currently I'm using prototype variables, but that is kind of unhandy and a
difference in usage between Pascal and C I want to avoid, if possible at
all.
TIA,
Marc
More information about the fpc-pascal
mailing list