{{fbdoc item="title" value="Operator New"}}----
Operator to dynamically allocate memory and construct data of a specified type.

{{fbdoc item="syntax"}}##
	[[KeyPgDeclare declare]] [[KeyPgOperator operator]] **new** ( //size// [[KeyPgAs as]] [[KeyPgUinteger uinteger]] ) [[KeyPgAs as]] [[KeyPgAny any]] [[KeyPgPtr ptr]]
	[[KeyPgDeclare declare]] [[KeyPgOperator operator]] **new[]** ( //size// [[KeyPgAs as]] [[KeyPgUinteger uinteger]] ) [[KeyPgAs as]] [[KeyPgAny any]] [[KeyPgPtr ptr]]
##
{{fbdoc item="usage"}}##
	//result// = **New** //[[DataType datatype]]//
		//or//
	//result// = **New** //[[DataType datatype]]// ( //initializers//, ... )
		//or//
	//result// = **New** //[[DataType datatype]]//**[** //count// **]**
##
{{fbdoc item="param"}}
	##//size//## 
		Number of bytes to allocate.
	##//initializers//##
		Initial value(s) for the variable.
	##//datatype//##
		Name of the data type to create.
	##//count//##
		Exact number of elements to allocate.

{{fbdoc item="ret"}}
	A pointer of type [[DataType datatype]] to the newly allocated data.

{{fbdoc item="desc"}}
	The ##**New**## operator dynamically allocates memory and constructs a specified data type. For simple types, like integers, an initial value can be given. For types without constructors, initial values can be specified for each field. Types that have constructors can have their constructors called by ##**New**## as well. If no initializers are given, the default values for those types will be set.

	##**New[]**## is the array-version of the ##**New**## operator and allocates enough memory for the specified number of objects.  The default constructor for the type will be used to set the initial values for each item.

	Objects created with ##**New**## must be freed with ##[[KeyPgOpDelete Delete]]##. Memory allocated with ##**New[]**## must be freed with ##**Delete[]**##, the array-version of ##[[KeyPgOpDelete Delete]]##. You cannot mix and match the different versions of the operators.

	Specifying an initial value of ##[[KeyPgAny Any]]##, as in ##**New** //datatype//(**Any**)## will allocate memory for the type, but not initialize the data.  This is only valid on data types that do not have constructors (otherwise for data types with constructors, syntax of simple memory allocation with pointer conversion, like //Cptr(datatype Ptr, Allocate(Sizeof(datatype)))//, can be substituted to the invalid use of New...Any).

	Specifying an initial value of ##[[KeyPgAny Any]]##, as in ##**New** //datatype//[//count//]{**Any**}## will allocate memory for the array, but not initialize the data.  This is only valid on data types that do not have constructors (otherwise for data types with constructors, syntax of simple memory allocation with pointer conversion, like //Cptr(datatype Ptr, Allocate(count * Sizeof(datatype)))//, can be substituted to the invalid use of New...Any).

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/operator/new.bas"}}%%(freebasic)
type Rational
	as integer	numerator, denominator
end type

scope

	' Create and initialize a "rational" and store its address.
	dim p as Rational ptr = new Rational(3, 4)

	print p->numerator & "/" & p->denominator

	' Destroy the rational and give its memory back to the system. 
	delete p

end scope

scope

	' Allocate memory for 100 integers and store the address of the first one.
	dim p as integer ptr = new integer[100]

	' Assign some values to the integers in the array.
	for i as integer = 0 to 99
		p[i] = i
	next

	' Free the entire integer array.
	delete[] p

end scope
%%
{{fbdoc item="lang"}}
	- Only available in the //[[CompilerOptlang -lang fb]]// dialect.

{{fbdoc item="diff"}}
	- New to ""FreeBASIC""

{{fbdoc item="see"}}
	- ##[[KeyPgOpDelete Delete]]##
	- ##[[KeyPgOpPlacementNew Placement New]]##

{{fbdoc item="back" value="CatPgOpMemory|Memory Operators"}}{{fbdoc item="back" value="CatPgOperators|Operators"}}