Navigation: Language Reference > 3 - Variable Declarations > Data Declarations and Memory Allocation >====== NEW (allocate heap memory) ====== | ![]() ![]() ![]() |
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
END
AnimalRef &Animal !A reference to any Animal CLASS
NameQ QUEUE
Name STRING(30)
END
QueRef &NameQ !A reference to any QUEUE with only a STRING(30)
CODE
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
CODE
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:
MyBaseClass CLASS,PUBLIC
X LONG,PUBLIC
Invoke PROCEDURE()
END
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
END
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.
Example:
C CLASS
T PROCEDURE(LONG), LONG
END
CODE
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}
Example:
Arr BYTE[,]
CODE
Arr &= NEW BYTE[,]1_2_4_5_6_7 !creates an array of dim(3,2)
!and initializes the elements
See Also: DISPOSE