Memory allocation

.malloc

C.malloc(size: num) -> ptr

Allocate memory block

Params (1)

size

Size of memory block, in bytes.

Return value

On success, a pointer to the memory block allocated by the function.

If size is zero or the function failed to allocate the requested block of memory, null is returned.

.calloc

C.calloc(count: num, size: num) -> ptr

Allocate and zero-initialize array

Params (2)

count

Number of elements to allocate.

size

Size of each element.

Return value

Like C.malloc, but the returned memory block is zero-initialized.

.realloc

C.realloc(addr: ptr, size: num) -> ptr

Reallocate memory block

Params (2)

addr

Pointer to a memory block previously allocated with C.malloc, C.calloc or C.realloc.

If this is a null pointer, the function works like C.malloc.

size

  • New size for the memory block, in bytes.

Return value

See C.malloc.

.free

C.free(addr: ptr)

Deallocate memory block

Parameters (1)

addr

A block of memory previously allocated by a call to C.malloc, C.calloc or C.realloc is deallocated, making it available again for further allocations.

Return value

None.

circle-info

Must free pointers created by malloc, calloc and realloc to avoid memory leaks.

triangle-exclamation

Last updated