answersLogoWhite

0


Best Answer

ss

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can delete operator used to prevent memory leak?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What will happen if you allocate memory using new and free it using free or allocate sing calloc and free it using delete?

The C++ new uses malloc internally to allocate memory and the C++ delete uses free internally to revoke memory. However, they are not interchangeable and so memory allocated with new MUST be revoked with delete. If you mix them up, you will have a memory leak! Haya.


What is caused when an application does not properly release memory allocated to it that it no longer needs and continually requests more memory than it needs?

memory leak


What is the difference between malloc and new other than syntax?

Click on the link to your right for the answer.Answerboth malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*).but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak. AnswerBesides the basic syntactical difference: malloc is a C function which will allocate the amount of memory you ask and that's it. new is a C++ operator which will allocate memory AND call the constructor of the class for which's object memory is being allocated. Similarily, free is a C function which will free up the memory allocated. but delete is a C++ operator which will free up the allocated memory AND call the destructor of the object.Answermalloc in a function and new is an operator, and its a good programming practice to use an operator instead of functions, because function itself requires some time to be executed whenever that particular function is called. so the main difference is that we use operators instead of malloc because of the TIME CONSTRAINT. Answer1.malloc requires the type casting during decleration , where as new doesn't needed the type casting during decleration 2. when ever we use new for allocating memory along with this it calls the constructor of the class for which object memory is allocated 3. in case of malloc free is the word used to clear the memory, where as delete is the format used in case of new to free the memory after usage 4. malloc is function, where as new is operator..so the time required for execution is less in case of new (it being a operator) as compared to malloc(it being a function) Answer1. malloc is a function call, while new is an operator. This difference is syntactic; behind the scenes, they both perform pretty much the same work to allocate the memory, and operator new also invokes any required constructors. There is a commonplace urban myth that operators are somehow faster in your code than functions; this is not correct, as any operator (except for mathematical operations that correspond directly to a single machine-code instruction) invocation amounts to a function call in any case. 2. malloc can fail, and returns a NULL pointer if memory is exhausted. Operator new never returns a NULL pointer, but indicates failure by throwing an exception instead. There is also a nothrow() version of operator new, which does return NULL on failure.


What is the difference between destructors and delete?

Delete is a built-in operator used forcibly release a dynamically allocated resource. Delete can only be used on pointer types, including pointer arrays. A destructor is a class method used to clean up any resources acquired by objects of the class. Destructors are invoked automatically from the most-derived class to the least-derived class, as soon as an object falls from scope. When you delete a pointer to an object, the object's destructor sequence is invoked. Note that you must not delete named objects otherwise you end up with a null reference. References must never be null.


What happens when class libraries leak memory in c plus plus?

A memory leak is when allocated memory that is no longer needed is not deallocated. Eventually, the memory pool is unable to satisfy an allocation request, and the program fails. A memory leak is a programming bug. When class libraries leak memory, they need to be fixed, just like any other piece of code that has bugs. If they came from a vendor, then that vendor needs to fix them.


What is java memory leak?

That means a memory leak in a program written in Java. A memory leak means that as the program runs, more and more memory is wasted - usually by being assigned and not de-assigned again. In Java this is not as usual as in other languages, since unused memory is normally reclaimed automatically by the garbage collector.


How do you fix a memory leak?

reboot the machine


What is memory leak problemin java?

Memory leaks do not occur in Java as the garbage collector clears the memory which has no references.


How do you overload new operator?

Here's an example of what it would look like to overload new and delete for a particular class. class Myclass { public: void* operator new(size_t); void operator delete(void*); }; Both of them are by default static members and do not maintain a this pointer. Overloading can be used for many purposes. For example, we may need to alter the exception thrown in case of failure--std::bad_alloc--and throw something else: void* Myclass::operator new(size_t size) { void *storage = malloc(size); if(NULL == storage) { throw "allocation fail : no free memory"; } } Usually we do this in a base class in order to have the functionality of the overloaded new in all derived classes. Implicitly the definition of new is included in every file, but in order to use the size_t declaration and other new related types you must include the header .The new operator can be implemented using malloc or another C function, called realloc, that handles memory allocation: void * realloc ( void * ptr, size_t size ); This can be used to change the size of the allocated block at address ptr with the size given in the second parameter. The address pointed to by ptr can actually be changed and the block moved someplace else, in which case the new address will be the returned value. If realloc fails, like malloc, it returns NULL. But realloc will not free the original memory if your memory allocation request fails. Therefore, when you use realloc, be sure to save the previous pointer value in case allocation fails, so that you do not leak memory.Note that in general, if you overload new, you will likely need to overload delete, and vice versa, because by changing how you allocate memory, you will typically also change how you free memory.Operator new is invoked implicitly when new is called; there is no syntax for calling operator new explicitly. #include #include #include using namespace std;class MyClass {int x, y;public:MyClass() {x = y = 0;}MyClass(int lg, int lt) {x = lg;y = lt;}void show() {cout


How do you ensure refrigerant gas does not leak from window air conditioner?

There is nothing you can do to prevent a leak from occurring.


What causes a memory leak?

A memory leaks can occur in a computer when a program on one's computer improperly manages the computer's memory allocations. This may happen when a object is stored but cannot be accessed by a running code.


If you notice that performance slows after a system has been up and running without a restart for some time would you suspect a memory leak?

True, you can assume that a memory leak is what is causing you problems.