Thread: please explain it.

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    please explain it.

    the following code produces a run time error. but i don't really understand How?
    Code:
    #include <iostream>
    using namespace std;
    
    class ID {
    	int id;
    public:
    	ID(int i) { id = i; }
    	void del() { delete this; }
    	int getID() { return id; }
    };
    
    int main()
    {
    	ID id(10);
    	id.del();
    	cout << id.getID() << endl;
    
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	void del() { delete this; }
    This is like sitting on the branch you are cutting from the tree [cutting inside yourself].
    http://www.srpska-mreza.com/History/...n/self-cut.jpg

    You should NEVER do "delete this".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could get away with it, but it certainly never is a good thing, especially if allocated on the stack.
    You have two undefined behavior there. Delete this on a class allocated on the stack and trying to access the class's contents after it's been deleted. No wonder you get a crash.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As Elysia correctly points out, doing "delete this" when "this" is a pointer to an object that was not created with new, then you are ALSO performing something bad in this situation - that would definitely cause a big problem in one way or another. I should have spotted that too.

    But it's also not particularly meaningful to do delete this in a case where the object has been allocated using new.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    But it's also not particularly meaningful to do delete this in a case where the object has been allocated using new.
    I think that depends. You can find solutions where it might work, such as MFC's CWinThread, which, by default, deletes itself when the thread has finished executing. But I would like to rely more on smart pointers, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks matsp and Elysia!
    I was thinking ... if I don't access any data member after doing delete this then it will work fine!
    But in this case, as Elysia pointed, that will delete a memory which was never newed.
    So I think it will still fail.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I could say yes to both statements. But delete itself can be a dangerous practice, since it can leave a dangling pointer somewhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You will. But you actually do access the data members.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 01:39 PM
  3. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:20 AM
  4. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM