CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    How to delete ptr from placement-new?

    Hi Gurus,

    I know, I must be a dork for not knowing it. But how do you delete a pointer created with placement-new? I'm trying to get with the sample below and I keep thinking that there should be a placement version of delete that correctly calls the dtor.

    Is my code wrong?
    Is there a right version of delete?

    Thanks. Sincerely, Chris.

    Code:
    #include <memory>
    #include <iostream>
    
    typedef unsigned int UINT8;
    
    class Something
    {
    public:
      Something() { }
      ~Something()
      {
        std::cout << "Something's dtor." << std::endl;
      }
    };
    
    void DoSomething(void)
    {
      // Get a local buffer.
      UINT8 buffer[sizeof(Something)];
    
      // Create ps in the local buffer.
      Something* ps = new((void*) buffer) Something();
    
      // Do something with ps.
    
      // Delete ps.
      ps->~Something();
    }
    
    int main(void)
    {
      DoSomething();
    }
    You're gonna go blind staring into that box all day.

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

    Re: How to delete ptr from placement-new?


  3. #3
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Re: How to delete ptr from placement-new?

    Quicker than a blitz as usual, Paul.
    Thanks.
    You're gonna go blind staring into that box all day.

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