1.

Explain the use of new and delete operators in pointers.

Answer»

a. The new operator in C++ is used for dynamic storage allocation. This operator can be used to create an object of any type.
General syntax of the new operator in C++:
The general syntax of a new operator in C++ is as follows:
pointer variable = new datatype; In the above statement, new is a keyword and the pointer variable is a variable of type datatype.

For example:
int * a = new int;
In the above example, the new operator allocates sufficient memory to hold the object of datatype int and returns a pointer to its starting point. The pointer variable holds the address of memory space allocated.

b. The delete operator in C++ is used for releasing memory space when the object is no longer needed.
General syntax of delete operator in C++:
delete pointer_variable;
For example, delete cptr;
In the above example, delete is a keyword and the pointer variable cptr is the pointer that points to the objects already created in the new operator.



Discussion

No Comment Found

Related InterviewSolutions