Skip to content

Instantly share code, notes, and snippets.

@JensMertelmeyer
Created April 5, 2022 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JensMertelmeyer/2691a4b373ce7091ec011daac394b961 to your computer and use it in GitHub Desktop.
Save JensMertelmeyer/2691a4b373ce7091ec011daac394b961 to your computer and use it in GitHub Desktop.
program Project1;
function getArrayRefCount(const firstElement): Integer;
const
SIZE_OF_ELEMENTCOUNT = SizeOf(NativeInt);
SIZE_OF_REFCOUNT = SizeOf(Integer);
OFFSET = 0 - (SIZE_OF_ELEMENTCOUNT + SIZE_OF_REFCOUNT);
var
ptr: TPtrWrapper;
begin
ptr := TPtrWrapper.Create(@firstElement);
Result := TMarshal.ReadInt32(ptr, OFFSET);
end;
var
words: TArray<Word>;
bytes: TArray<Byte> absolute words;
otherWords: TArray<Word>;
otherBytes: TArray<Byte>;
begin
ReportMemoryLeaksOnShutdown := True;
words := [$0FF0, $FEFE, $0100];
WriteLn( 'Array ref count: ', getArrayRefCount(words[0]) );
WriteLn( 'Array ref count: ', getArrayRefCount(bytes[0]) );
otherWords := words;
Write(sLineBreak);
WriteLn( 'Array ref count: ', getArrayRefCount(words[0]) );
WriteLn( 'Array ref count: ', getArrayRefCount(bytes[0]) );
otherBytes := bytes;
Write(sLineBreak);
WriteLn( 'Array ref count: ', getArrayRefCount(words[0]) );
WriteLn( 'Array ref count: ', getArrayRefCount(bytes[0]) );
readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment