Memory allocation functions

Normally, memory for variables is allocated automatically. Alternatively, you can allocate variables in dynamic memory (heap) using these functions.

 

These functions can be used to allocate and free memory for variables of any types, including composite types (str, VARIANT, etc) and types that have constructors/destructors. They are similar to C++ operators new and delete. To simply allocate memory also can be used dll functions or str variables. See also safe arrays.

 

Syntax

p._new([count])

p._resize([count])

p._delete

int p._len

 

Parameters

p - pointer variable of any type.

count - number of elements. For _new, default is 1. For _resize, default is plus 1, and can be negative to add -count elements.

 

Remarks

_new allocates memory for single variable or array of type of p. All bytes are set to 0. If the type has constructor, it is called for each element. The function sets p to point to the first element. Returns reference to p. When the memory is not needed anymore, it must be freed using function _delete. The function does not free previously allocated memory.

 

_resize resizes memory to hold more or less elements. Whole or part of previous content is preserved. If count is greater than previous number of elements, the added memory is initialized in same way as with _new. If count is less, the deleted elements are freed in same way as with _delete. Variable p must point to memory allocated with _new or _resize, or be 0. The function sets p to point to the first element of resized and possibly moved memory block. Returns reference to p.

 

_delete frees memory and sets p to 0. If the type has destructor, it is called for each element. Elements of str and other composite types also are properly cleaned. Variable p must point to memory allocated with _new or _resize, or be 0. The function returns reference to p.

 

_len returns number of elements in array. Variable p must point to memory allocated with _new or _resize, or be 0.

 

Memory allocated using these functions is not automatically freed when the pointer variable is destroyed. You must use _delete to free it.

 

If a pointer variable uses these functions, it cannot use other memory-allocation functions, and vice versa.

 

Examples

str* p._new(10)
out p._len ;;10
p._resize(20)
out p._len ;;20
p._delete

MyClass* c._new
...
c._delete