Alloc v0.1.0

Source
Allocates an uninitialized block of memory.

Package: Memory

Signature

func Alloc(
    size: uint
) -> *var opaque;

Parameters

NameTypeDescription
sizeuintThe number of bytes to allocate.

Returns

A pointer to the first byte of the block, or null if the platform could not satisfy the request. The bytes are not zeroed — they hold whatever was last left there, so read them only after writing them, or clear the block with Zero first.

The block belongs to the caller and must be released exactly once with Free. Every successful call returns a distinct block, and the pointer stays valid until it is passed to Free or moved by Realloc.

Example

import Memory::{ Alloc, Free, Zero };

func Main() -> int {
    let buffer = Alloc(1024);
    if buffer == null {
        return 1;
    }

    Zero(buffer, 1024);
    Free(buffer);
    return 0;
}

See also

  • Memory — the package overview
  • Free — release the block when it is no longer needed
  • Realloc — resize a block that already exists
  • Zero — clear the uninitialized bytes