Like Tree1Likes
  • 1 Post By Elkvis

Is this safe?

This is a discussion on Is this safe? within the C++ Programming forums, part of the General Programming Boards category; My computer is broken, so I can't test this; is it safe to delete this; on a class that may ...

  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
    21,794
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,282
    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
    2,462
    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.
    MK27 likes this.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21