# %malloc() function

The %malloc() function allocates a memory
block of size bytes and returns the address of this block.

## Syntax

```
var = (char*)%malloc(size)
```

## Description

The memory is freed by the %free() built-in. If the system fails to allocate memory,
a null pointer is returned.

If the FlashBASIC program terminates,
the space allocated by %malloc is not released
automatically. This feature can be used to create static data, live
across program executions. The TCL environ command
uses this fact.

Memory allocated by %malloc can be freed by exiting the D3 process.

## Example(s)

```
ptr=(char*)%malloc(2048)
if ptr=0 then stop ’cannot allocate memory’
```

## See also

- [%free() function](https://d3codex.com/pickbasic-flashbasic/percent-free-function/)
- [%putenv() function](https://d3codex.com/pickbasic-flashbasic/percent-putenv-function/)
- [%read() function](https://d3codex.com/pickbasic-flashbasic/percent-read-function/)
- [cfunction statement](https://d3codex.com/pickbasic-flashbasic/cfunction-statement/)
- [disc command](https://d3codex.com/tcl/disc-command/)
- [environ command](https://d3codex.com/tcl/environ-command/)

---
Source: https://d3codex.com/pickbasic-flashbasic/percent-malloc-function/ - part of the D3Codex reference.
