User Tools

Site Tools

Navigation:  Language Reference > 3 - Variable Declarations > Data Declarations and Memory Allocation >====== NEW (allocate heap memory) ====== Previous pageReturn to chapter overviewNext page

reference &= NEW( datatype )


reference The label of a reference variable that matches the datatype.
NEW Creates a new instance of the datatype on the heap.
datatype The label of a previously declared CLASS or QUEUE structure, or any simple data type declaration. This may contain a variable as the parameter of the data type to allow truly dynamic declarations.

The NEW statement creates a new instance of the datatype on the heap. NEW is only valid on the source side of a reference assignment statement. Memory allocated by NEW is automatically initialized to blank or zero when allocated, and must be explicitly de-allocated with the DISPOSE statement (else you'll create a “memory leak”).

The use of parentheses around the datatype is optional.

Example for WIN32 based code:

StringRef &STRING             !A reference to any STRING variable

LongRef   &LONG               !A reference to any LONG variable

Animal CLASS

Feed    PROCEDURE(short amount)

Weight  LONG


AnimalRef &Animal             !A reference to any Animal CLASS


Name   STRING(30)


QueRef  &NameQ                !A reference to any QUEUE with only a STRING(30)


AnimalRef &= NEW(Animal)     !Create new instance of an Animal class

QueRef &= NEW(NameQ)         !Create new instance of a NameQ QUEUE

StringRef &= NEW(STRING(50)) !Create new STRING(50) variable

X# = 35                      !Assign 35 to a variable and then

StringRef &= NEW(STRING(X#)) !use that variable to Create a new STRING(35)

LongRef &= NEW(LONG)         !Create new LONG variable


The following information below applies ONLY to the Clarion# language.


Clarion# - NEW as Operator or Modifier

In Clarion#, the NEW keyword can be used as an operator or as a modifier.

NEW Operator

The NEW operator is used to create objects and invoke constructors, for example:

MyClass  Class1


 MyClass = NEW Class1()

If the NEW operator fails to allocate memory, it throws the “OutOfMemory” exception.

NEW Modifier

Use the NEW modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name and signature, and then apply the NEW modifier to the declaration.

Consider the following class:


X            LONG,PUBLIC

Invoke       PROCEDURE()


Declaring a method with the name Invoke in a derived class and using the NEW modifier will hide the Invoke method in the base class.

For example:

MyDerivedClass  CLASS(MyBaseClass),PUBLIC

X                LONG,PUBLIC

Invoke           PROCEDURE(),PUBLIC,NEW


It is an error to use both NEW and DERIVED on the same member. Using the NEW modifier in a declaration that does not hide an inherited member generates a warning.

Clarion# - NEW as Operator in Expression

Clarion# supports the syntax implemented in Clarion 6.0. But, in Clarion# you can also use NEW as an operator in any expression.







  I# = (NEW C).T(3)

Clarion# - Creating arrays with NEW

In Clarion#, it is now possible to dynamically create arrays with optional initializers. The syntax of this type of operator is as follows:

Variant without initilizers:

NEW <; elementType >[dim1,',dimN]

Variant with initializers:

NEW <; elementType >[,',]{initdim1elem1, initdim1elem2,',initdim1elemN}


Arr BYTE[,]


Arr &amp;= NEW BYTE[,]1_2_4_5_6_7 !creates an array of dim(3,2)

                                      !and initializes the elements


new_allocate_heap_memory_.htm.txt · Last modified: 2021/04/15 15:57 by