What happens if you make call "delete this;"?, C/C++ Programming

The code contains two built-in pitfalls. First one, if it executes in a member function for an static, extern or automatic object, the program will possibly crash as soon as the delete statement executes. There is not any portable way for an object to tell that it was instantiated on the heap, thus the class cannot assert that its object is correctly instantiated. Second one, while an object commits suicide this way, using program may not know about its demise. So far as the instantiating program is concerned, the object remains in scope & continues to exist although the object did itself in. Usually Subsequent dereferencing of the pointer can & does lead to disaster.

You must never do this. As compiler does not know whether object was allocated onto the stack or on the heap, "delete this" could cause a tragedy.

 

Posted Date: 3/16/2013 2:58:47 AM | Location : United States







Related Discussions:- What happens if you make call "delete this;"?, Assignment Help, Ask Question on What happens if you make call "delete this;"?, Get Answer, Expert's Help, What happens if you make call "delete this;"? Discussions

Write discussion on What happens if you make call "delete this;"?
Your posts are moderated
Related Questions
Question : (a) Define a Pointer. Provide an example of an integer pointer variable. (b) Give advantages of using pointers over arrays. (c) Declare an integer pointer

Imagine that the server program is a setuid program owned by the root, then after you penetrate the program with a shell running, you become the ROOT and you can remove the whole f

It is a pointer accessible only in the member functions of a struct, class or union type. It points to the object for which the member function is called. Static member functions d

#questionAt a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets

Constructor public class ListNode {    // package access members; List can access these directly private E data; // data for this node privateListNode nextNode; /

construct a console programme for a bank ATM machine.


At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers


At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes