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

Hybrid View

  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
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Using delete on an object without new

    Quote Originally Posted by exterminator View Post
    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).
    you're only getting yourself into problems with that type of model.
    It's easy to work around. One method: prevent local copies (make the constructors private) and have a static object creation function.

  6. #6
    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...

  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
    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.

  11. #11
    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.

  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
    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

  14. #14
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Using delete on an object without new

    Quote Originally Posted by Paul McKenzie View Post
    there is no way to delete something that isn't a pointer value
    Well this could be what he's after, but delete on a stack based object has to be undefined behaviour I think.
    Code:
    class X
    {
    public:
    operator X*()
    {
    returnthis;
    }
    };
    int main()
    {
    X x;
    delete x;
    }
    
    PS:- How comes copy/paste from my msvc9 express ruins all indentation. Ive set to use spaces instead of tabs like on all previous versions but my code never cut/pastes properly??
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Using delete on an object without new

    Quote Originally Posted by Gorbo View Post
    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!
    Please tell your instructor/teacher he is an idiot.
    Tell him I said so.
    And tell him to come here to come explain in what even remotely realistic setting this is ever going to be an actual need.


    Other than "syntax games" (which really has NOTHING to do with C++ at all) there is no reason to ever do something like this. If this is part of a C++ curriculum other than training in said "syntax games", I stand by my statement he's an idiot.

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