Skip to content
IsaacShelton edited this page Mar 21, 2022 · 1 revision

New

One way to dynamically allocate memory, is using new followed by the name of a type.

new

new Person

new undef

By default, memory allocated with new with be zero-initialized. If this is undesired, you can insert undef after the new keyword in order to leave the allocated memory undefined.

new undef Person

new Type * count

For allocating blocks of memory of a certain type, a count can be specified by appending * followed by a value containing the number of elements desired.

new int * 10
new float * (num_vertices * 3)

new vs malloc()

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

New Heap-Allocated C-String (deprecated)

To easily create a heap-allocated C-String, you can use new followed by a C-String literal.

new 'Hello World'

The resulting value will be a *ubyte which points to the newly allocated and initialized C-String.

Other ways of Dynamically Allocating

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

See here for more information

Clone this wiki locally