CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Oct 2008
    Posts
    45

    Using delete on an object without new

    Hi,

    I need a special class X to make this work without errors
    so what am i looking for besides overloading delete?

    void main()
    {
    X x;
    delete x;
    }

    thanks
    Last edited by Gorbo; December 29th, 2008 at 12:03 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    Hi,

    I need a special class X to make this work
    Why? What purpose does instantiating an object, and then calling "delete" serve?

    Also, main() returns int, not void.
    Code:
    struct SomeX
    {
    };
    
    typedef SomeX* X;
    
    int main()
    {
      X x;
      x = 0;
      delete x;
    }
    That is the only way to do what you want. The delete requires that the argument is a pointer. Since it must be a pointer, the X is a typedef for a pointer to some real class. Also, I had to set it to 0, so that issuing the delete call doesn't lead to undefined behaviour.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    so what am i looking for besides overloading delete?

    void main()
    {
    X x;
    delete x;
    }
    1. main returns an int, always.
    2. What you're after is completely unintuitive. If you explain your reasoning, we can suggest proper solutions.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Using delete on an object without new

    One ocassion where in I wanted something like that was when I had 2 code paths and each led to either a pointer to an object on the stack or the free-store and it was a tough to find out when it was pointing to what and correspondingly choose to call delete or not (without adding a state). So, I opted to change the naked pointer usage with a shared_ptr with the deleter set to a noop routine which I used in case the pointer was to the stack object else it had the default deleter as delete in cases it were pointing to the free store. But, I did not need to use delete explicitly as the OP is requesting for. So, not sure what he actually needs that for.
    Last edited by exterminator; December 29th, 2008 at 04:48 AM.

  5. #5
    Join Date
    Oct 2008
    Posts
    45

    Re: Using delete on an object without new

    So main is void instead of int big deal it's not a real program just a test program and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...

    anyway i hope i cleared things up and you can point me to the right direction now...

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Using delete on an object without new

    You must overload operator new() before overload operator delete().
    Thanks for your help.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    So main is void instead of int big deal it's not a real program just a test program and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...

    anyway i hope i cleared things up and you can point me to the right direction now...
    Sentences are a good thing.

    That doesn't clear anything up as you didn't provide an explanation as to why it's required. Let's try again. Why do you need to call delete on a stack allocated object? What are you trying to accomplish?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    So main is void instead of int big deal it's not a real program just a test program
    Test programs are real programs.
    and i need the code unchanged all changes must be in class X why do i need it well it's an assignment so no reason really just because it's what needs to be done...
    You're responding to professional programmers who have decades of experience here -- don't expect that we won't ask you questions on what you're really trying to do.

    Operator delete requires a pointer. That class instance is not a pointer. Therefore there is no way you can get that code to compile unless X is a pointer -- that's why the typedef was needed.

    Unless you can change the rules of the C++ language, you're stuck. And because you're stuck is indeed the reason why we want to know what you're really trying to accomplish.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Using delete on an object without new

    Just as an FYI....

    I know a decent number of instructors (including secondary school teachers, and university procfessors) who will (correctly) FAIL and assignment with main returning a void.

    When I give training courses (and I have a course specifically for teachers) I stringly encourage them to adopt this attitude.

    A peice of code which has main returning a void, is NOT a valid C++ program. PERIOD.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Oct 2008
    Posts
    45

    Re: Using delete on an object without new

    Lets try this again

    i do not know the purpose of this program i do not know what i am trying to accomplish
    all i know is i was given this code and was told to make it work by creating my own X class that's it!

    now if you know how to accomplish this please respond if not don't bother.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo
    i do not know the purpose of this program i do not know what i am trying to accomplish
    all i know is i was given this code and was told to make it work by creating my own X class that's it!
    Sounds like a useless assignment from a misguided teacher. Anyway, Paul McKenzie's suggestion of a typedef should work, if by "work" you mean "compile". It may or may not run "correctly" since there would be undefined behaviour, but I am not sure if you can actually avoid that without changing the main function itself.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    Lets try this again

    i do not know the purpose of this program i do not know what i am trying to accomplish
    all i know is i was given this code and was told to make it work by creating my own X class that's it!

    now if you know how to accomplish this please respond if not don't bother.
    What you're asking makes no sense as many of us have coded for decades without the need and it's not defined in the language. You don't know why you need to do it, so the only real logical conclusion is you have a serious design flaw or you don't understand the requirements.

    Stack allocated objects get deleted when they go out of scope. You just need to define the scope appropriately so that the objects exist only as long as they're needed.

    Objects allocated on the heap using new exist until they're explicitly deleted, which is why you need to delete them.

    What you're asking shows that you, or whoever is asking you to do this, lacks an understanding of how C++ and its memory management works. You can keep asking and keep getting frustrated, but it won't change the fact that it just doesn't work that way, and if you understood how it does work, you'd realize what you're asking for makes no sense.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Using delete on an object without new

    Quote Originally Posted by GCDEF
    What you're asking makes no sense as many of us have coded for decades without the need and it's not defined in the language. You don't know why you need to do it, so the only real logical conclusion is you have a serious design flaw or you don't understand the requirements.
    From what Gorbo has stated, I believe the problem is with the requirements. The assignment question is fundamentally flawed.

    Speaking of changing the rules of the language, another "creative" way of solving the assignment question besides using a typedef might be:
    Code:
    #define delete ;
    
    class X{};
    
    int main()
    {
        X x;
        delete x;
    }
    I cannot seem to tell from the C++ standard if redefining delete in this way is not allowed, results in undefined behaviour, or is actually okay, but seeing that the assignment is just wrong one might as well submit this as an "answer".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    Lets try this again

    i do not know the purpose of this program i do not know what i am trying to accomplish
    all i know is i was given this code and was told to make it work by creating my own X class that's it!
    So you didn't have any idea that what they are asking you to do is designed to waste your time? That's what it seems like to me, because you cannot do what you say you want to do. It's like asking us to divide by zero and give you a numerical answer.
    now if you know how to accomplish this please respond if not don't bother.
    Again, and this is the last time, there is no way to delete something that isn't a pointer value, so no one can tell you how to accomplish it. What we can do is explain why it cannot be done, which we have painstakingly done, but you refuse to listen.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Using delete on an object without new

    Quote Originally Posted by laserlight View Post
    I cannot seem to tell from the C++ standard if redefining delete in this way is not allowed, results in undefined behaviour, or is actually okay, but seeing that the assignment is just wrong one might as well submit this as an "answer".
    I don't have a copy of the standard on me, but I can tell you that that's UB.

    Another solution:
    Code:
    struct X
    {
        operator X*() const {return 0;}
    };
    
    int main()
    {
        X x;
    
        delete x;
    }
    Like in the typedef example demonstrated by Paul, the invocation of scalar/vector operator delete on a null pointer is guaranteed to be a logical no-op.
    Last edited by Plasmator; December 29th, 2008 at 04:04 PM. Reason: Fixed typo.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured