Recent

Author Topic: Managed pointers  (Read 689 times)

jksmithiii

  • New member
  • *
  • Posts: 9
Managed pointers
« on: December 13, 2024, 07:49:37 pm »
Surely this has already been beaten on, but a pointer type with Initialize and Finalize operators. Of value or no?

cdbc

  • Hero Member
  • *****
  • Posts: 1784
    • http://www.cdbc.dk
Re: Managed pointers
« Reply #1 on: December 13, 2024, 08:25:52 pm »
Hi
AFAICR 'AllocMem' initializes allocated memory to zeroes before returning it to you. ...And there's 'FreeMemAndNil', which deallocates mem sets the pointer to nil.
Code: Pascal  [Select][+][-]
  1. var
  2.   ManagedMem = array of byte; // dynamic array
  3. begin
  4.   setlength(ManagedMem,100); // uninitialized
  5.   ... use your lump of 100 bytes mem ...
  6.   // since dynamic arrays are managed types,
  7.   // this deallocates when going out of scope
  8. end;
  9.  
Otherwise, @Thaddy did something clever with 'SmartPointer', you'll have to search the forum for that...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

jksmithiii

  • New member
  • *
  • Posts: 9
Re: Managed pointers
« Reply #2 on: December 13, 2024, 08:42:29 pm »
Pointer types with implicit initialize and finalize when pointer goes in/out of scope. That's the built-in safety value. Could write a lib around that using pointer records I suppose.

 

TinyPortal © 2005-2018