Skip to content
IsaacShelton edited this page Nov 13, 2022 · 3 revisions

Malloc

One way to dynamically allocate memory, is using malloc() (as opposed to new).

malloc(size_in_bytes)

Requirements

In order to use malloc(), you must import one or more of the following:

  • 2.7/cstdlib.adept (recommended)
  • sys/cstdlib.adept
  • 2.7/basics.adept

Result Value

The result value will be a ptr to the newly heap-allocated memory.

Usage with sizeof

Since malloc() operates in terms of bytes, it is often necessary to use sizeof in combination with malloc().

malloc(10 * sizeof int)

new vs malloc()

The difference between new and malloc(), is that new will zero-initialize the allocated memory by default.

Other ways of Dynamically Allocating

You can optionally use new/delete instead of malloc()/free()

See here for more information

Clone this wiki locally