3

In C++, there exists an inplace-new operator to create an object using preallocated memory. Is there an equivalent option in Delphi?

cytrinox
  • 1,846
  • 5
  • 25
  • 46
  • Something similar or perhaps duplicate, [Is there a generic “Object Pool” implementation for Delphi?](http://stackoverflow.com/q/16404051/576719) and [Boosting Work Classes with a mini Object Pool](http://www.delphitools.info/2013/09/26/boosting-work-classes-with-a-mini-object-pool/). – LU RD Apr 04 '15 at 20:02

1 Answers1

2

There is no direct equivalent (that I know of), but you can achieve much the same effect (controlling how and where memory for an object type is allocated) by overriding the NewInstance class function in your custom class and providing an implementation, which provides the space for the new instance from a custom heap pool.

  • Well, the idea was a flexible way to create any class on memory already allocated by an external allocator (which can call a callback function for destruction). NewInstace overrides can't provide this. But if there is no other chance I'll try it. – cytrinox Apr 04 '15 at 16:48
  • NewInstance can do that but it requires a rather messy way to get the address of the pre allocated memory to NewInstance. – David Heffernan Apr 05 '15 at 07:40