Thread: Is this safe?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Is this safe?

    My computer is broken, so I can't test this; is it safe to delete this; on a class that may or may not have been allocated with new? For example:

    Code:
    class Potpie{
    Potpie(Ingrediants);
    void Bake(unsigned int);
    ~Potpie(){
    delete this; // May/may not have been allocated with new
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    30
    ...So if it wasn't allocated with new, I shouldn't do it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    30
    Dammit... alright, and thanks for the eerily instantaneous reply o.0

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    If you can explain why you were wanting to do that we may have an alternative strategy.

    Soma

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    actually, what you have there is an infinitely recursive situation. delete always calls the destructor, which calls delete, etc. if you created an object of that type on the stack in a function, the function would never return because as that object went out of scope, the destructor would get called, and try to delete itself. you would eventually run out of stack space and definitely crash. the only way your code will work, is if you allocate with new, and never EVER use delete on those objects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Safe Input for Hex
    By akeel88 in forum C Programming
    Replies: 3
    Last Post: 10-31-2010, 04:56 AM
  2. Is this safe?
    By pheres in forum C++ Programming
    Replies: 22
    Last Post: 04-01-2008, 03:02 PM
  3. Is this safe?
    By caduardo21 in forum C Programming
    Replies: 55
    Last Post: 06-30-2005, 09:01 AM
  4. How Safe is your pc
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-16-2003, 02:53 AM
  5. How safe is it?
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-08-2002, 09:33 PM