Placement operator new/delete question

Discussion in 'C++' started by Ioannis Vranos, Oct 16, 2004.

  1. When we use the standard placement new operator provided in <new>, and
    not a definition of owr own, isn't a call to placement delete enough?


    Consider the code:


    #include <new>


    class SomeClass{};

    int main()
    {
    using namespace std;

    unsigned char garbage[sizeof(SomeClass)];

    SomeClass *t = new(garbage) SomeClass;

    delete t;
    }



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #1
    1. Advertising

  2. Ioannis Vranos

    David Hilsee Guest

    "Ioannis Vranos" <> wrote in message
    news:1097885170.258380@athnrd02...
    > When we use the standard placement new operator provided in <new>, and
    > not a definition of owr own, isn't a call to placement delete enough?
    >
    >
    > Consider the code:
    >
    >
    > #include <new>
    >
    >
    > class SomeClass{};
    >
    > int main()
    > {
    > using namespace std;
    >
    > unsigned char garbage[sizeof(SomeClass)];
    >
    > SomeClass *t = new(garbage) SomeClass;


    The above is generally not portable because of alignment issues. This is
    more portable:

    int main()
    {
    unsigned char* garbage = new unsigned char[sizeof(SomeClass)];

    SomeClass *t = new(garbage) SomeClass;

    t->~SomeClass();
    delete[] garbage;
    }

    I'm speaking in general terms about an arbitrary class named SomeClass.
    There may be some "special case" for empty classes of which I'm not aware.
    See http://www.gotw.ca/gotw/028.htm.

    > delete t;
    > }


    What do you mean by a call to "placement delete"? What is "placement
    delete"? You code doesn't seem to be doing anything out of the ordinary
    here. This looks like you're trying to deallocate the array "garbage",
    which smacks of undefined behavior. Why would you do this? If you just
    want to call SomeClass's destructor, then just need to write

    t->~SomeClass();

    as I have written above.

    --
    David Hilsee
     
    David Hilsee, Oct 16, 2004
    #2
    1. Advertising

  3. Ioannis Vranos

    Daniel T. Guest

    In article <1097885170.258380@athnrd02>,
    Ioannis Vranos <> wrote:

    > When we use the standard placement new operator provided in <new>, and
    > not a definition of owr own, isn't a call to placement delete enough?
    >
    >
    > Consider the code:
    >
    >
    > #include <new>
    >
    >
    > class SomeClass{};
    >
    > int main()
    > {
    > using namespace std;
    >
    > unsigned char garbage[sizeof(SomeClass)];
    >
    > SomeClass *t = new(garbage) SomeClass;
    >
    > delete t;
    > }


    It seems to me that the above code is rather bad. You are calling delete
    on a block of code that is sitting on the stack (ie it wasn't newed.)

    I would think it should be written like this:

    int main() {
    unsigned char garbage[sizeof(SomeClass)];
    SomeClass* t = new( garbage ) SomeClass;

    t->~SomeClass();
    }
     
    Daniel T., Oct 16, 2004
    #3
  4. David Hilsee wrote:

    > The above is generally not portable because of alignment issues. This is
    > more portable:
    >
    > int main()
    > {
    > unsigned char* garbage = new unsigned char[sizeof(SomeClass)];
    >
    > SomeClass *t = new(garbage) SomeClass;
    >
    > t->~SomeClass();
    > delete[] garbage;
    > }



    What alignment issues? I can't see any difference between the two forms.



    > What do you mean by a call to "placement delete"? What is "placement
    > delete"? You code doesn't seem to be doing anything out of the ordinary
    > here. This looks like you're trying to deallocate the array "garbage",
    > which smacks of undefined behavior. Why would you do this? If you just
    > want to call SomeClass's destructor, then just need to write
    >
    > t->~SomeClass();
    >
    > as I have written above.






    References on "placement operator delete":

    C++2003 18.4.1.3


    TC++PL 3: Page 576, 19.4.5.




    VS help says about the placement delete form:

    "The second function is called by a placement delete expression
    corresponding to a new expression of the form new(std::size_t). It does
    nothing."


    In another place:

    "The second and third forms of this operator will commonly not be called
    from code but exist to give the compiler a matching delete to call when
    a placement new fails."


    So if I got it right, placement delete does not do anything if it not
    explicitly defined, but is provided as a match when exceptions are
    thrown etc.

    So indeed we have to call the destructor explicitly. However experiments
    with my compilers puzzle me.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #4
  5. Daniel T. wrote:

    > It seems to me that the above code is rather bad. You are calling delete
    > on a block of code that is sitting on the stack (ie it wasn't newed.)
    >
    > I would think it should be written like this:
    >
    > int main() {
    > unsigned char garbage[sizeof(SomeClass)];
    > SomeClass* t = new( garbage ) SomeClass;
    >
    > t->~SomeClass();
    > }



    At first I must mention that I have began to remember!


    You are right. However more elegantly we can do:


    #include <cstddef>
    #include <new>
    #include <iostream>


    class SomeClass
    {
    static size_t occupied;
    static unsigned char *buffer;

    public:

    void *operator new(size_t size)
    {
    using namespace std;

    cout<<"Class member placement operator new was called!\n";

    static const size_t MAX_SIZE=5*1024;

    if(occupied+size>MAX_SIZE)
    throw bad_alloc();

    occupied+=size;

    return buffer+occupied-size;
    }

    void operator delete(void *p)
    {
    std::cout<<"Class member placement operator delete was called!\n";

    SomeClass *temp=static_cast<SomeClass *>(p);

    temp->~SomeClass();

    occupied-=sizeof(SomeClass);
    }
    };

    size_t SomeClass::eek:ccupied=0;
    unsigned char *SomeClass::buffer=new unsigned char[5*1024];


    int main()
    {
    SomeClass *p=new SomeClass;

    delete p;
    }



    Run the code and see.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #5
  6. Ioannis Vranos wrote:
    > David Hilsee wrote:
    >
    >> The above is generally not portable because of alignment issues. This is
    >> more portable:
    >>
    >> int main()
    >> {
    >> unsigned char* garbage = new unsigned char[sizeof(SomeClass)];
    >>
    >> SomeClass *t = new(garbage) SomeClass;
    >>
    >> t->~SomeClass();
    >> delete[] garbage;
    >> }

    >
    >
    >
    > What alignment issues? I can't see any difference between the two forms.


    This is very subtle. Pointers returned by new are usually guarenteed
    (not by the standard but by the implementation) to be aligned by the
    most demanding type supported by that platform. Objects allocated by
    the compiler (on the stack or globals) are aligned by the requirements
    of the type being allocated. That's why David used new.

    >
    > So indeed we have to call the destructor explicitly. However experiments
    > with my compilers puzzle me.
    >


    Show us which experiments puzzle you.
     
    Gianni Mariani, Oct 16, 2004
    #6
  7. Ioannis Vranos wrote:
    >
    > Daniel T. wrote:
    >
    >> It seems to me that the above code is rather bad. You are calling
    >> delete on a block of code that is sitting on the stack (ie it wasn't
    >> newed.)
    >>
    >> I would think it should be written like this:
    >>
    >> int main() {
    >> unsigned char garbage[sizeof(SomeClass)];
    >> SomeClass* t = new( garbage ) SomeClass;
    >>
    >> t->~SomeClass();
    >> }

    >
    >
    >
    > At first I must mention that I have began to remember!
    >
    >
    > You are right. However more elegantly we can do:
    >
    >
    > #include <cstddef>
    > #include <new>
    > #include <iostream>
    >
    >
    > class SomeClass
    > {
    > static size_t occupied;
    > static unsigned char *buffer;
    >
    > public:
    >
    > void *operator new(size_t size)
    > {
    > using namespace std;
    >
    > cout<<"Class member placement operator new was called!\n";
    >
    > static const size_t MAX_SIZE=5*1024;
    >
    > if(occupied+size>MAX_SIZE)
    > throw bad_alloc();
    >
    > occupied+=size;
    >
    > return buffer+occupied-size;
    > }
    >
    > void operator delete(void *p)
    > {
    > std::cout<<"Class member placement operator delete was called!\n";
    >
    > SomeClass *temp=static_cast<SomeClass *>(p);
    >
    > temp->~SomeClass();
    >
    > occupied-=sizeof(SomeClass);
    > }
    > };
    >
    > size_t SomeClass::eek:ccupied=0;
    > unsigned char *SomeClass::buffer=new unsigned char[5*1024];
    >
    >
    > int main()
    > {
    > SomeClass *p=new SomeClass;
    >
    > delete p;
    > }
    >
    >
    >
    > Run the code and see.



    I want to mention here, that the code is a demonstration only, and does
    not really work reliably for more than one objects.


    In summary there are two cases of placement operator new and delete:

    1) Global placement new, new[], delete, and delete[].

    2) Class-oriented member functions placement new, new[], delete and
    delete[].


    The last are also implicitly called as the globals.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #7
  8. Ioannis Vranos

    David Hilsee Guest

    "Ioannis Vranos" <> wrote in message
    news:1097893186.834843@athnrd02...
    > David Hilsee wrote:
    >
    > > The above is generally not portable because of alignment issues. This

    is
    > > more portable:
    > >
    > > int main()
    > > {
    > > unsigned char* garbage = new unsigned char[sizeof(SomeClass)];
    > >
    > > SomeClass *t = new(garbage) SomeClass;
    > >
    > > t->~SomeClass();
    > > delete[] garbage;
    > > }

    >
    >
    > What alignment issues? I can't see any difference between the two forms.


    Alignment is a hairy issue. The link that I provided discussed it a little
    bit. It's also discussed in the FAQ in 11.10. In a nutshell, functions
    like malloc() and the allocation functions in C++ (3.7.3.1) are required to
    return a pointer that can be converted to a pointer of any complete object
    type and dereferenced, used, etc. If you simply have a locally defined
    array of unsigned char, there is no such requirement that it must work. In
    the above code, I'd probably avoid the usage of unsigned char and simply use
    operator new, the bad_alloc-throwing C++ memory allocation function that
    resembles malloc, directly.

    > > What do you mean by a call to "placement delete"? What is "placement
    > > delete"? You code doesn't seem to be doing anything out of the ordinary
    > > here. This looks like you're trying to deallocate the array "garbage",
    > > which smacks of undefined behavior. Why would you do this? If you just
    > > want to call SomeClass's destructor, then just need to write
    > >
    > > t->~SomeClass();
    > >
    > > as I have written above.

    >
    >
    >
    >
    >
    > References on "placement operator delete":
    >
    > C++2003 18.4.1.3
    >
    >
    > TC++PL 3: Page 576, 19.4.5.
    >
    >
    >
    >
    > VS help says about the placement delete form:
    >
    > "The second function is called by a placement delete expression
    > corresponding to a new expression of the form new(std::size_t). It does
    > nothing."
    >
    >
    > In another place:
    >
    > "The second and third forms of this operator will commonly not be called
    > from code but exist to give the compiler a matching delete to call when
    > a placement new fails."
    >
    >
    > So if I got it right, placement delete does not do anything if it not
    > explicitly defined, but is provided as a match when exceptions are
    > thrown etc.
    >
    > So indeed we have to call the destructor explicitly. However experiments
    > with my compilers puzzle me.


    Oh, placement _operator_ delete. When people refer to "placement new"
    they're usually referring to what the standard calls a "new expression"
    (e.g. new (pointer) Type()), and not operator new, so I assumed you were
    referring to some unusual "delete expression". The wording's a bit tricky,
    but there is a difference. You're not calling placement operator delete in
    the above code. As you can see in the standard, placement operator delete
    "intentionally performs no action". I'm not really sure why things are the
    way they are, but you can see that the placement operator news and placement
    operator deletes do nothing. I suspect that they exist to make the wording
    in other parts of the standard easier on the reader.

    Yes, you're right, you need to call the destructor explicitly. The code "T*
    p = new T();" performs two major steps: it allocates storage for an instance
    of T and (assuming the allocation succeeded and the constructor exists) it
    invokes the constructor for T. In the code you provided, step 1 was
    unnecessary, so all that is needed to "clean up" is a call to the
    destructor.

    --
    David Hilsee
     
    David Hilsee, Oct 16, 2004
    #8
  9. Ioannis Vranos

    David Hilsee Guest

    "Gianni Mariani" <> wrote in message
    news:...
    > Ioannis Vranos wrote:
    > > David Hilsee wrote:
    > >
    > >> The above is generally not portable because of alignment issues. This

    is
    > >> more portable:
    > >>
    > >> int main()
    > >> {
    > >> unsigned char* garbage = new unsigned char[sizeof(SomeClass)];
    > >>
    > >> SomeClass *t = new(garbage) SomeClass;
    > >>
    > >> t->~SomeClass();
    > >> delete[] garbage;
    > >> }

    > >
    > >
    > >
    > > What alignment issues? I can't see any difference between the two forms.

    >
    > This is very subtle. Pointers returned by new are usually guarenteed
    > (not by the standard but by the implementation) to be aligned by the
    > most demanding type supported by that platform. Objects allocated by
    > the compiler (on the stack or globals) are aligned by the requirements
    > of the type being allocated. That's why David used new.


    I'm not sure what you mean when you say that it is guaranteed by the
    implementation but not by the standard. My understanding is that the code I
    provided is required by the standard to call operator new[], and operator
    new[] must return a pointer that is properly aligned for any type.
    Therefore, the code should be guaranteed by the standard to work. However,
    I think using operator new directly would be a little clearer.

    --
    David Hilsee
     
    David Hilsee, Oct 16, 2004
    #9
  10. David Hilsee wrote:

    > Alignment is a hairy issue. The link that I provided discussed it a little
    > bit. It's also discussed in the FAQ in 11.10. In a nutshell, functions
    > like malloc() and the allocation functions in C++ (3.7.3.1) are required to
    > return a pointer that can be converted to a pointer of any complete object
    > type and dereferenced, used, etc. If you simply have a locally defined
    > array of unsigned char, there is no such requirement that it must work. In
    > the above code, I'd probably avoid the usage of unsigned char and simply use
    > operator new, the bad_alloc-throwing C++ memory allocation function that
    > resembles malloc, directly.




    Yes, however here are both are used as unsigned char * so this doesn't
    apply here. For example we do not do something like:


    SomeClass *p=static_cast<SomeClass *>(new unsigned char[100]);



    > Oh, placement _operator_ delete. When people refer to "placement new"
    > they're usually referring to what the standard calls a "new expression"
    > (e.g. new (pointer) Type()), and not operator new, so I assumed you were
    > referring to some unusual "delete expression".




    As far as I know this is the default global placement operator new,
    without an explicit user-definition.





    > The wording's a bit tricky,
    > but there is a difference. You're not calling placement operator delete in
    > the above code. As you can see in the standard, placement operator delete
    > "intentionally performs no action".



    The default one.




    > I'm not really sure why things are the
    > way they are, but you can see that the placement operator news and placement
    > operator deletes do nothing. I suspect that they exist to make the wording
    > in other parts of the standard easier on the reader.




    The default global placement operator new just places an object in the
    address passed to it.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #10
  11. Ioannis Vranos wrote:

    > I want to mention here, that the code is a demonstration only, and does
    > not really work reliably for more than one objects.
    >
    >
    > In summary there are two cases of placement operator new and delete:
    >
    > 1) Global placement new, new[], delete, and delete[].
    >
    > 2) Class-oriented member functions placement new, new[], delete and
    > delete[].
    >
    >
    > The last are also implicitly called as the globals.



    Also an additional distinction here. The ones used in the example, are
    explicit user definitions of operators new and delete in the member
    function call.


    "Placement operators new/new[]/delete/delete[]" the ones used in the style

    SomeClass *p=new(somepointer) SomeClass;


    have the signatures and form:

    void* operator new (size_t, void* p) throw() { return p; }

    void operator delete (void* p, void*) throw() { }

    void* operator new[](size_t, void* p) throw() { return p; }

    void operator delete[](void* p, void*) throw() { }



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #11
  12. Ioannis Vranos wrote:

    >> Oh, placement _operator_ delete. When people refer to "placement new"
    >> they're usually referring to what the standard calls a "new expression"
    >> (e.g. new (pointer) Type()), and not operator new, so I assumed you were
    >> referring to some unusual "delete expression".

    >
    >
    >
    >
    > As far as I know this is the default global placement operator new,
    > without an explicit user-definition.




    What I said applies. And a user can define his own versions of placement
    operators.

    However the ones I use in my code example lower in the thread, are
    user-defined simple operators new and delete, not the "placement" ones.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 16, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Olcott
    Replies:
    11
    Views:
    2,771
    David Rubin
    Mar 1, 2004
  2. Jef Driesen
    Replies:
    1
    Views:
    506
    Gernot Frisch
    Jan 19, 2005
  3. Mark P
    Replies:
    6
    Views:
    811
    James Dennett
    Apr 27, 2005
  4. LuB
    Replies:
    15
    Views:
    598
    James Kanze
    Jun 20, 2007
  5. zr
    Replies:
    5
    Views:
    608
Loading...

Share This Page