Name
ReallocMem Procedure
Syntax
procedure ReallocMem(var P: Pointer; NewSize: Integer);
Description
ReallocMem changes the size of a dynamically
allocated block, preserving as much of the previous contents as
possible.
If P is nil,
ReallocMem is just like GetMem.
If NewSize is zero, it is just like
FreeMem, which means P is not
changed, so it contains an invalid pointer after
ReallocMem returns.
When possible, ReallocMem tries to adjust the size
of the current memory block, but if it cannot, it allocates an
entirely new block of the desired size and copies the old data into
the new block. In either case, if NewSize is
bigger than the current size, the extra memory is not initialized.
ReallocMem is not a real procedure.
Tips and Tricks
If you have a block that grows incrementally, anticipate future growth. Instead of frequently increasing the block size in small steps, increase the size by larger steps less often. Try to call
ReallocMemas little as possible becauseReallocMemoften fails to reuse the current block. As the block size grows, the performance penalty for copying the old block to the new block also grows.ReallocMemin Delphi’s default memory manager is thread-safe, that is, you can callReallocMemfrom multiple threads simultaneously, but only ifIsMultiThreadis True.Delphi’s memory manager assumes that the program will frequently allocate and free blocks of varying sizes. Most programs fit this pattern, but a few don’t. If your program often calls
ReallocMemto incrementally ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access