answersLogoWhite

0

Explain the purpose of new and delete operator?

Updated: 8/10/2023
User Avatar

Sureshkumar25us

Lvl 1
12y ago

Best Answer

The new operator instantiates a named object of a given type while the delete operator destroys an object. The new operator invokes the object's default constructor unless directed to invoke a specific constructor. The delete operator always invokes the object's destructor.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

C++ supports dynamic allocation and deallocation of objects using the new and delete operators. These operators allocate memory for objects from a pool called the free store. The new operator calls the special function operator new, and the delete operator calls the special function operator delete.

In Visual C++ .NET 2002, the new function in the Standard C++ Library will support the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception if the memory allocation fails.

The C Runtime Library's new function will also throw a std::bad_alloc exception if the memory allocation fails.

If you still want the non-throwing version of new for the C Runtime Library, link your program with nothrownew.obj. However, when you link with nothrownew.obj, new in the Standard C++ Library will no longer function.

For a list of the library files that comprise the C Runtime Library and the Standard C++ Library, see C Run-Time Libraries.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

new operator is used for allocating a memory.delete is used for releasing a memory.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain the purpose of new and delete operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Explain how objects are created in Java?

with new operator


Can memory which has been allocated by new be freed by free?

No, you have to use the operator delete to objects created by new.


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


How can you can create a new operator through operator overloading?

You cannot create any new operators in C++. You can only overload the existing ones (although some, such as sizeof, new and delete cannot be overloaded). The only way to create a new operator is to implement it as a standard function with a named identifier. For instance, sqrt() is the standard library function that provides the square root operator, for which no real operator exists.


What operator is a unary operator as it works with only one operand?

Yes, a unary operator is an operator that only has one operand. Examples of unary operators are negative (-), positive (+), increment (++), decrement (--), address of (&), dereference (*), logical not (!), sizeof, one's complement (~), new, and delete.


How are the 'new' and 'delete' operators used?

You use the new operator to instantiate an object dynamically (at runtime) upon the heap (free store). You use the delete operator to release those same dynamic allocations when they are no longer required. The new and delete operators are similar to the malloc and free functions used in C (and which are still used today to implement these operators in the background). However, everything you could do with malloc and free you can also do with new and delete, including the instantiation of primitives, and is the preferred method of doing so in C++.


When does a transfer occur?

A cold transfer occurs on a phone with an operator when they cannot complete your request. They then hand you off to another operator, except they do not inform them of your issue, which means that you have to re-explain everything to the new operator.


How do you delete variables that are dynamically allocated?

It depends, especially if you are going to use C++.If you allocated the variable using the malloc call or any of its derivatives you must use the corresponding 'free' subroutine call to delete them.If you use the more modern C++ 'new' operator, then use the 'delete' operator to remove the memory dynamically in the program.


Program for new and delete operator using c plus plus?

#include<iostream> struct object { int m_data; }; void main() { object obj=new object; obj.m_data = 42; delete( obj ); return( 0 ); }


When does a cold transfer occur?

A cold transfer occurs on a phone with an operator when they cannot complete your request. They then hand you off to another operator, except they do not inform them of your issue, which means that you have to re-explain everything to the new operator.


Write c plus plus program for new and delete operator using class?

#include<iostream> class foo{ int m_data; }; int main() { foo* p=new foo; delete( foo), foo=NULL; return(0); }