Table of Contents
- 1 What happens when delete is used for a null pointer?
- 2 Do I need to set pointer to NULL after delete?
- 3 What happens when delete is called?
- 4 What is the value of a pointer after delete?
- 5 What happens if the following C++ statement is compiled and executed int * ptr null delete PTR?
- 6 What happens when u delete this operator?
- 7 Is it safe to delete a null pointer from a function?
- 8 What happens if you delete a pointer to an object?
What happens when delete is used for a null pointer?
What happens when delete is used for a NULL pointer? Question 4 Explanation: Deleting a null pointer has no effect, so it is not necessary to check for a null pointer before calling delete.
What happens if delete p is called when p is NULL?
No! The C++ language guarantees that delete p will do nothing if p is null. Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test.
Do I need to set pointer to NULL after delete?
Setting pointers to NULL following delete is not universal good practice in C++. There are times when it is a good thing to do, and times when it is pointless and can hide errors. There are plenty of circumstances where it wouldn’t help. But in my experience, it can’t hurt.
What happens when delete is used for a null pointer int * ptr NULL Deleteptr?
1) new is an operator, malloc is a function 2) new calls constructor, malloc doesn’t 3) new returns appropriate pointer, malloc returns void * and pointer needs to typecast to appropriate type….
Q. | What happens when delete is used for a NULL pointer? int *ptr = NULL;delete ptr; |
---|---|
C. | no effect |
Answer» c. no effect |
What happens when delete is called?
Using the delete operator on an object deallocates its memory. When delete is used to deallocate memory for a C++ class object, the object’s destructor is called before the object’s memory is deallocated (if the object has a destructor).
What is a delete operator?
Explanation: The delete operator is the reverse process of a new operator. It deallocates all the memory allocated for an object. The object can be of any type. The delete operator completely destroys an object so that the resources can be used for other purposes.
What is the value of a pointer after delete?
Answer #1: The pointer itself does have an address and the value. The address of the pointer does not change after you perform delete on it. The space allocated to the pointer variable itself remains in place until your program releases it (which it might never do, e.g. when the pointer is in the static storage area).
Can we use a pointer after delete?
Yes, it’s totally fine to reuse a pointer to store a new memory address after deleting the previous memory it pointed to. Just be careful not to dereference the old memory address which is still stored in the pointer.
What happens if the following C++ statement is compiled and executed int * ptr null delete PTR?
10. What happens if the following C++ statement is compiled and executed? Explanation: The above statement is syntactically and semantically correct as C++ allows the programmer to delete a NULL pointer, therefore, the program is compiled and executed successfully.
What is the purpose of the delete operator?
The delete operator removes a given property from an object. On successful deletion, it will return true , else false will be returned.
What happens when u delete this operator?
“delete this” in C++ 1) delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefined. Take a step-up from those “Hello World” programs.
What is role of delete operator Mcq?
Is it safe to delete a null pointer from a function?
Yes it is safe. There’s no harm in deleting a null pointer; it often reduces the number of tests at the tail of a function if the unallocated pointers are initialized to zero and then simply deleted. Since the previous sentence has caused confusion, an example — which isn’t exception safe — of what is being described:
What happens if you set a pointer to null twice?
Setting a pointer to NULL after deleting it masquerades memory allocation errors, which is a very bad thing. A program that is correct does not delete a pointer twice, and a program that does delete a pointer twice shouldcrash. – Damon Aug 30 ’13 at 18:48
What happens if you delete a pointer to an object?
Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed. The delete operator has void return type does not return a value. Here, Below are examples where we can apply delete operator: 1. Deleting Array Objects: We delete an array using [] brackets. CPP.
Do you have to check for null before deleting a string?
In conclusion, although a sloppy implementation of operator delete may require explicit null checks in the client code, this is non-standard behaviour and should only be tolerated in legacy support ( if at all ). Deleting null is a no-op. There’s no reason to check for null before calling delete.