Mixing new/delete and operator new/delete?

Discussion in 'C++' started by Jef Driesen, Jan 19, 2005.

  1. Jef Driesen

    Jef Driesen Guest

    Is it allowed by the C++ standard to mix the use of 'new/delete' and
    'operator new/delete'?

    (a) alloc (version 1) + free (version 2)
    (b) alloc (version 2) + free (version 1)


    // VERSION 1 ALLOC (using operator new + placement new)
    type* data = (type*)operator new(n * sizeof(type));
    for (type* p = data; p != data + n; ++p)
    new ((void *)p) value_type();

    // VERSION 1 FREE (using calling destructor + operator delete)
    for (type* p = data; p != data + n; ++p)
    p->~type();
    operator delete(data);

    // VERSION 2 ALLOC (using new)
    type* data = new type[n];

    // VERSION 2 FREE (using delete)
    delete [] data;
     
    Jef Driesen, Jan 19, 2005
    #1
    1. Advertising

  2. "Jef Driesen" <> schrieb im Newsbeitrag
    news:csll9c$ofa$...
    > Is it allowed by the C++ standard to mix the use of 'new/delete' and
    > 'operator new/delete'?
    >
    > (a) alloc (version 1) + free (version 2)
    > (b) alloc (version 2) + free (version 1)
    >
    >
    > // VERSION 1 ALLOC (using operator new + placement new)
    > type* data = (type*)operator new(n * sizeof(type));
    > for (type* p = data; p != data + n; ++p)
    > new ((void *)p) value_type();
    >
    > // VERSION 1 FREE (using calling destructor + operator delete)
    > for (type* p = data; p != data + n; ++p)
    > p->~type();
    > operator delete(data);
    >
    > // VERSION 2 ALLOC (using new)
    > type* data = new type[n];
    >
    > // VERSION 2 FREE (using delete)
    > delete [] data;


    I can imagine it might now cause much problems, as new and delete
    internally call malloc, however I would not rely on that + there is no
    rule this has to be so. Thus, this can crash badly.
    -Gernot
     
    Gernot Frisch, Jan 19, 2005
    #2
    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:
    393
    Alf P. Steinbach
    Aug 6, 2003
  2. Matt Garman

    mixing operator new, free()

    Matt Garman, Apr 2, 2004, in forum: C++
    Replies:
    6
    Views:
    1,133
    Geoffrey Mortimer
    Apr 8, 2004
  3. Amy
    Replies:
    13
    Views:
    6,022
    Kurt Stutsman
    Feb 23, 2005
  4. tom
    Replies:
    5
    Views:
    409
  5. xmllmx
    Replies:
    6
    Views:
    417
    xmllmx
    Feb 3, 2010
Loading...

Share This Page