new operator is used to dynamically allocate memory on the heap. Memory allocated by new must be deallocated using delete operator.
Syntax of new is:
p_var = new type name;
where p_var is a previously declared pointer of type typename. typename can be any basic data type.
new can also create an array:
p_var = new type [size];
In this case, size specifies the length of one-dimensional array to create.
It allocates memory space for an integer variable. And allocated memory can be released using following statement,
a = new int;
It creates a memory space for an array of 100 integers. a will refer to the first element, a to the second element, and so on. And to release the memory following statement can be used,
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
Copyright©2012 electrofriends.com All Rights Reserved