-
-
Notifications
You must be signed in to change notification settings - Fork 9
New
One way to dynamically allocate memory, is using new
followed by the name of a type.
new Person
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
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)
The difference between new
and malloc()
, is that new
will zero-initialize the allocated memory by default.
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.
You can optionally use malloc()
/free()
instead of new
/delete
See here for more information