NEW


General Forms:

symid = NEW recordtype [ USING poolid]
symid = NEW classtype [ USING poolid]
symid = NEW PTR(ptrtype) [ USING poolid]
symid = NEW OBJECT [ USING poolid]
Where:
symid = a numeric expression identifying the dynamic
recordtype = a numeric expression identifying the record.
classtype = a class name.
ptrtype = an expression identifying the type of the object.
poolid = a numeric expression identifying the specific pool to use or a DEFSTRUCT already allocated from the pool.

Memory pools provide a local memory heap that can be used to dynamically allocate string, structure or objects using the NEW keyword. NEW returns a numeric handle identifying the new object which can be accessed using a FLD() or SYM(*) function.

If the object to be allocated is a structure you can use the simple form of NEW thus:

	pEmp = NEW _employee

Ideally the structure should be defined with DEFSTRUCT rather than DEFRECORD as only the former will initialize the fields correctly.

It is also possible to allocate the same structure using the more general form of NEW as in this example:

	pEmp = NEW PTR(X$_employee)

The X$ is a dummy variable and has no effect on the program. You can declare dynamic arrays this way with:

	pHist = NEW PTR(X())
	REDIM SYM(*pHist)(10)
	SYM(*pHist)(1)=1

The allocated array had no size when allocated so you need a REDIM to give it dimensions.

The handle remains valid until either the pool goes out of scope or the object is explicitly freed with DELETE PTR.

By default allocations are made from the memory pool for the currently executing function. As that pool is released when the function exits, it is normal for helper functions to specify that a pool in their calling functions should be used so that the allocation can persist once the function exits. This is done with the USING clause which can refer to either a specific pool handle, as returned by GET POOL, or by just specifying a DEFSTRUCT previously allocated from that pool.

Compatibility

Introduced with KCML 7.00.

Syntax examples:

pRec = NEW _myRec
pRecArray = NEW PTR(X$()_myRec)
h = NEW OBJECT
a = NEW _listnode USING listhdr$ a = NEW myclass

See also:

DELETE PTR(),
POOL,
DEFSTRUCT,
DEFCLASS,
FLD(),
Tutorial on memory pooling