Manual memory management - Factor Documentation (original) (raw)

Sometimes data passed to C functions must be allocated at a fixed address. See Byte arrays and the garbage collector for an explanation of when this is the case.

Allocating a C datum with a fixed address:

The libc vocabulary defines several words which directly call C standard library memory management functions:

calloc ( count size -- alien )
realloc ( alien size -- newalien )

You must always free pointers returned by any of the above words when the block of memory is no longer in use:

The above words record memory allocations, to help catch double frees and track down memory leaks with Destructor tools. To free memory allocated by a C library, another word can be used:

Utilities for automatically freeing memory in conjunction with with-destructors:

|free ( alien -- alien )

The &free and |free words are generated using Alien destructors.

You can unsafely copy a range of bytes from one memory location to another:

You can copy a range of bytes from memory into a byte array: