Placing an instance of a class on the stack or in any memory area lazarus / 21-General ---------------------------------------------------------------------- ALLIGATOR · 2025-05-08 17:51 I was interested in the task of making class instances be placed in any memory location, so far I have come to this code. Who is interested in this topic, I suggest to evaluate, criticize, give advice, improve the code, or maybe even say that it's all dabbling and unnecessary  (in fact, it's a dabble for me so far, but who knows, maybe something interesting will be born from it, so I share with you + boast  ) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program app;{$mode objfpc}{$modeswitch advancedrecords}{$h+} uses Generics.Collections; type  generic GetClass = class sealed  strict private type    TWrapperClass = class sealed(T)      _InstanceSize_: Boolean;    end;  public const    Size = UIntPtr(@TWrapperClass(nil)._InstanceSize_);  end;   generic TInplaceClass = class(T)  private type    TSelf = specialize TInplaceClass;    TSelfClass = class of TSelf;  strict private class threadvar    MemPointer: Pointer;  public    class function Inplace(mem: Pointer): TSelfClass; inline;    class function NewInstance: TObject; override;    procedure FreeInstance; override;  end;   generic TInlineClass = record  strict private type  TWrapperClass = specialize TInplaceClass;  strict private    space: array [0..specialize GetClass.Size-1] of Byte;  public    instance: TWrapperClass;    function get: TWrapperClass.TSelfClass; inline;  end; class function TInplaceClass.NewInstance: TObject;begin  InitInstance(MemPointer);  NewInstance := TObject(MemPointer);end; class function TInplaceClass.Inplace(mem: Pointer): TSelfClass;begin  MemPointer := mem;  Result := TSelf;end; procedure TInplaceClass.FreeInstance;beginend; function TInlineClass.get: TWrapperClass.TSelfClass;begin  instance := TWrapperClass(@space[0]);  Result := TWrapperClass.Inplace(@space[0]);end; var  m: specialize TDictionary;  inplace_class: specialize TInlineClass<(specialize TDictionary)>;  arr1: array [0..specialize GetClass>.Size-1] of byte;  arr2: array [0..1000] of byte; begin// ----  m := specialize TInplaceClass<(specialize TDictionary)>.Inplace(@arr1[low(arr1)]).Create;   m.Capacity:=1024;  m.Add(1,2);  m.Add(3,4);   WriteLn(UIntPtr(@arr1[low(arr1)]));  WriteLn(UIntPtr(m));  WriteLn;   m.Free; // ----  m := specialize TInplaceClass<(specialize TDictionary)>.Inplace(@arr2[low(arr2)]).Create;   m.Capacity:=1024;  m.Add(1,2);  m.Add(3,4);   WriteLn(UIntPtr(@arr2[low(arr2)]));  WriteLn(UIntPtr(m));  WriteLn;   m.Free; // ----  inplace_class.get.Create;   inplace_class.instance.Capacity:=1024;  inplace_class.instance.Add(1,2);  inplace_class.instance.Add(3,4);   WriteLn(UIntPtr(inplace_class.instance));  WriteLn(SizeOf(inplace_class));  WriteLn;   inplace_class.instance.Free;   ReadLn;end. ---------------------------------------------------------------------- Fibonacci · 2025-05-08 19:12 Nice, but what can it be used for? ---------------------------------------------------------------------- ALLIGATOR · 2025-05-08 19:17 I think this could be applied somewhere in high performance places - the point is that no memory manager is accessed For example, if a large number of objects are created and destroyed many times, this could speed things up... but then again, if it works at all, I'm not sure yet ---------------------------------------------------------------------- egsuh · 2025-05-09 02:32 AFAIK, Lazarus supports object, which is depricated in Delphi. It is managed within stack memory (again AFAIK). ---------------------------------------------------------------------- Thaddy · 2025-05-09 09:06 Yes, old school objects are stack oriented.. Normally classes are on the heap even if local to a procedure or function. After all classes are pointers. Although it is possible to declare them on the stack provided you write a new root class that overrides newinstance. I gave a demo a couple of years ago somewhere on the forum. (I remember an erratic comment on it, you can ignore that) It is based on this: http://www.atelierweb.com/index.php/64-bit- _alloca-how-to-use-from-delphi/ and combined with newinstance/initinstance. Note you have to read the constraints I defined: it is not a fix all/do all solution and 64 bit only. The alternative is using advanced records.