Comprehensive treatment of new / delete

Discussion in 'C++' started by Dave, Jul 18, 2005.

  1. Dave

    Dave Guest

    Hello all,

    I'd like to find a source on the web that discusses, in a comprehensive
    manner and in one place, everything about new / delete. It should include
    overloading operator new, the new operator, placement, nothrow, arrays,
    etc...

    My books cover the topic, I've found FAQs on the web that cover the topic,
    and so on, but all the sources I've found are disjointed. There's a bit on
    this page, a bit on that page, and so on. The information may all be there,
    but it's kind of hard to piece it all together.

    If anybody knows of an article out there that gives a comprehensive
    treatment of this topic in one place, I'd appreciate a link.

    Thanks,
    Dave
     
    Dave, Jul 18, 2005
    #1
    1. Advertising

  2. Dave wrote:
    > Hello all,
    >
    > I'd like to find a source on the web that discusses, in a comprehensive
    > manner and in one place, everything about new / delete. It should include
    > overloading operator new, the new operator, placement, nothrow, arrays,
    > etc...
    >
    > My books cover the topic, I've found FAQs on the web that cover the topic,
    > and so on, but all the sources I've found are disjointed. There's a bit on
    > this page, a bit on that page, and so on. The information may all be there,
    > but it's kind of hard to piece it all together.
    >
    > If anybody knows of an article out there that gives a comprehensive
    > treatment of this topic in one place, I'd appreciate a link.


    I don't know of one but let's see if we can make one !

    http://www.google.com/search?q=new delete&btnG=Search the C++ FAQ&sitesearch=www.parashift.com

    That will give you a fairly detailed treatment of the topic.

    The code below covers most of the new/delete usages I have used.

    ------------------------------------------------------------------------
    #include <unistd.h>
    #include <cstring>

    // non-portable but non-allocating write method
    void Write( const char * l_str )
    {
    ::write( 1, l_str, std::strlen( l_str ) );
    ::write( 1, "\n", 1 );
    }



    #include <new>
    #include <cstdlib>


    inline void * operator new ( size_t i_val )
    {
    Write( "new called" );

    void * l_ptr = std::malloc( i_val );
    return l_ptr;
    }

    inline void operator delete ( void * i_ptr )
    {
    Write( "delete called" );

    if ( i_ptr )
    {
    std::free( i_ptr );
    }
    }

    struct A
    {
    A()
    {
    Write( "A contructed" );
    }

    ~A()
    {
    Write( "A destructed" );
    }


    };

    template <typename X>
    void test1()
    {

    X * l_a = new X;

    delete l_a;

    X * l_b = new X[3];

    delete [] l_b;
    }


    // This does not seem to be called - no matter what
    inline void operator delete ( void * i_ptr, size_t i_val )
    {
    Write( "delete (with size) called" );

    if ( i_ptr )
    {
    std::free( i_ptr );
    }
    }


    template <typename X>
    void test2()
    {

    X * l_a = new X;

    delete l_a;

    X * l_b = new X[3];

    delete [] l_b;
    }


    struct B
    {

    inline void * operator new ( size_t i_val )
    {
    Write( "B::new called" );

    void * l_ptr = std::malloc( i_val );
    return l_ptr;
    }


    inline void operator delete ( void * i_ptr )
    {
    Write( "B::delete (with size) called" );

    if ( i_ptr )
    {
    std::free( i_ptr );
    }
    }

    B()
    {
    Write( "B contructed" );
    }

    ~B()
    {
    Write( "B destructed" );
    }


    };

    struct C
    {

    inline void * operator new ( size_t i_val )
    {
    Write( "C::new called" );

    void * l_ptr = std::malloc( i_val );
    return l_ptr;
    }


    inline void * operator new ( size_t i_val, void * i_ptr )
    {
    Write( "C::new placement new called" );

    return i_ptr;
    }


    inline void operator delete ( void * i_ptr, size_t i_val )
    {
    Write( "C::delete (with size) called" );

    if ( i_ptr )
    {
    std::free( i_ptr );
    }
    }

    C()
    {
    Write( "C contructed" );
    }

    ~C()
    {
    Write( "C destructed" );
    }


    };

    template <typename X>
    void test3()
    {

    long x[ ( sizeof( long ) + sizeof( X ) -1 )/sizeof( long ) ];

    X * l_x = new ( ( void * ) x ) X;

    l_x->~X();

    }



    int main()
    {
    Write( "\ntest1<A>" );
    test1<A>();

    Write( "\ntest2<A>" );
    test2<A>();

    Write( "\ntest1<B>" );
    test1<B>();

    Write( "\ntest1<C>" );
    test1<C>();

    Write( "\ntest3<A>" );
    test3<A>();

    Write( "\ntest3<C>" );
    test3<C>();

    }

    -------- output ----------

    ../new_del

    test1<A>
    new called
    A contructed
    A destructed
    delete called
    new called
    A contructed
    A contructed
    A contructed
    A destructed
    A destructed
    A destructed
    delete called

    test2<A>
    new called
    A contructed
    A destructed
    delete called
    new called
    A contructed
    A contructed
    A contructed
    A destructed
    A destructed
    A destructed
    delete called

    test1<B>
    B::new called
    B contructed
    B destructed
    B::delete (with size) called
    new called
    B contructed
    B contructed
    B contructed
    B destructed
    B destructed
    B destructed
    delete called

    test1<C>
    C::new called
    C contructed
    C destructed
    C::delete (with size) called
    new called
    C contructed
    C contructed
    C contructed
    C destructed
    C destructed
    C destructed
    delete called

    test3<A>
    A contructed
    A destructed

    test3<C>
    C::new placement new called
    C contructed
    C destructed
     
    Gianni Mariani, Jul 18, 2005
    #2
    1. Advertising

  3. Dave

    Alex Vinokur Guest

    Dave wrote:
    > Hello all,
    >
    > I'd like to find a source on the web that discusses, in a comprehensive
    > manner and in one place, everything about new / delete. It should include
    > overloading operator new, the new operator, placement, nothrow, arrays,
    > etc...

    [snip]
    > If anybody knows of an article out there that gives a comprehensive
    > treatment of this topic in one place, I'd appreciate a link.
    >
    > Thanks,
    > Dave


    Some samples with 'new'
    http://groups.google.com/group/comp.lang.c /msg/432c7f9231d4b03b

    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 2, 2005
    #3
    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. HeroOfSpielburg
    Replies:
    1
    Views:
    403
    Alf P. Steinbach
    Aug 6, 2003
  2. klaas
    Replies:
    3
    Views:
    504
    klaas
    Aug 22, 2003
  3. Jef Driesen
    Replies:
    1
    Views:
    518
    Gernot Frisch
    Jan 19, 2005
  4. Bart Simpson

    compiler treatment of const etc

    Bart Simpson, May 31, 2007, in forum: C++
    Replies:
    3
    Views:
    289
    Ron Natalie
    May 31, 2007
  5. X X
    Replies:
    4
    Views:
    365
    red floyd
    Jul 19, 2010
Loading...

Share This Page