ReAllocateMemory()

Syntax

*NewMemoryID = ReAllocateMemory(*MemoryID, Size [, Flags])
Description
Resizes the given memory buffer to a new size. The memory may be copied to a new location in the process if there is not enough memory available at its current location.

Parameters

*MemoryID The address of the memory area to resize. This value must be the result of a call to AllocateMemory() or ReAllocateMemory().

If this parameter is #Null, the command acts like AllocateMemory() and allocates a new memory area of the given size.
Size The size in bytes for the resized or allocated buffer.
Flags (optional) It can be one of the following values:
  #PB_Memory_NoClear: don't fill the extended memory area with zeros. It can help to have faster allocation if the
                      extended memory is used immediately. If the memory size is reduced, this flag has no effect.

Return value

Returns the new address of the memory area if it could be resized. In this case, the old '*MemoryID' address can no longer be used. If resizing the memory area failed (because there is not enough memory available), the result is zero, and the '*MemoryID' address is still valid with the existing memory area and the old size.

Remarks

If the size of the memory area is increased, any new bytes are initially filled with zeros unless the #PB_Memory_NoClear flag is specified.

If the program crashes at this command even though the input seems correct, it is usually a result of a memory corruption at an earlier time in the program by writing at an area outside of the allocated memory area. Such an error can be narrowed down to the real cause using the purifier debugger tool.

All remaining allocated memory blocks are automatically freed when the program ends.

Example

  *MemoryID = AllocateMemory(1000)
  PokeS(*MemoryID, "Store this string")
  ; do something more with it here...
  ;
  *NewMemoryID = ReAllocateMemory(*MemoryID, 2000) ; need more memory
  If *NewMemoryID
    ; work with *NewMemoryID now with size 2000
    Debug "The old contents are still here:"
    Debug PeekS(*NewMemoryID)
    ;
    FreeMemory(*NewMemoryID) 
  Else
    ; resizing failed, keep working with *MemoryID (size 1000)
    ;
    FreeMemory(*MemoryID)
  EndIf

See Also

AllocateMemory(), FreeMemory(), MemorySize()

Supported OS

All

<- PokeW() - Memory Index