The syntax for a local variable is absolute for another variable with some offset

Is there a way that I can declare a variable with an absolute address that has some bias to the variable it refers to. For example, instead of:

function RefCount(const s: string): Integer; begin Result := PInteger(Integer(s) - 8)^; end; 


Is there any way I can do:

 function RefCount(const s: string): Integer; var Count: PInteger absolute s {- 8 ?} ; begin Result := Count^; end; 

(The example is for illustration only; this is not necessarily useful.)

+6
source share
2 answers

No, I don’t think there is an "extended syntax" for the absolute keyword. The documentation is here , and as far as I know, there are no undocumented functions associated with this keyword.

+7
source

The syntax for your request does not exist.

However, you can use use-pointer arithmetic (if you are using a version that supports it), for example:

 function RefCount(const s: string): Integer; begin if s <> '' then Result := (PInteger(s) - 2)^; else Result := 0; end; 

A more robust approach is to use the StrRec record StrRec , which is what String really contains inside:

 function RefCount(const s: string): Integer; begin if s <> '' then Result := (PStrRec(s) - 1)^.refCnt else Result := 0; end; 

Or, an arithmetic version without a pointer:

 function RefCount(const s: string): Integer; begin if s <> '' then Result := PStrRec(LongInt(s) - SizeOf(StrRec))^.refCnt else Result := 0; end; 

BTW, starting with D2009 +, the System block has its own StringRefCount() function, which returns a row counter.

+4
source

Source: https://habr.com/ru/post/903956/


All Articles