malloc

<alloc>
function
[defunc [malloc [i32 size]] -> void*]
fn malloc(size: i32): void* {}
fn malloc(size: i32): void* {}

Allocate a block of memory

Allocates a block of memory of size bytes. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

The returned memory will NOT be freed automatically. You must call free to release the memory when you are done with it.

Parameters

  • size: The size of the memory block to allocate, in bytes.

Return value

On success, a pointer to the memory block is returned. On failure, NULL is returned.

Example

[define ptr [malloc 100]]
[if ptr
	[print "Memory allocated successfully"]
	[print "Memory allocation failed"]]
let ptr: void* = malloc(100);

if (ptr !== null) {
		print("Memory allocated successfully");
} else {
		print("Memory allocation failed");
}
let ptr: void* = malloc(100);

if (ptr !== null) {
		print("Memory allocated successfully");
} else {
		print("Memory allocation failed");
}

See Also

alloc/realloc Re-allocate a block of memory.
alloc/free Releases a block of memory.
alloc/premalloc Pre-allocates a block of memory.