Topic: operator new


Author: "Adrian Zajkeskovic" <no_address@fake.com>
Date: Sat, 27 Jul 2002 17:50:10 GMT
Raw View
While reading the MSDN I found the following paragraph:

<msdn>
template <> void AFXAPI ConstructElements <CPerson> ( CPerson* pNewPersons,
int nCount )
{
    for ( int i = 0; i < nCount; i++, pNewPersons++ )
    {
        // call CPerson default constructor directly
        new( pNewPersons )CPerson;
    }
}

This override iterates through the new CPerson objects, calling each
object's constructor. The special new operator used here constructs a new
CPerson object in place rather than allocating memory from the heap.
</msdn>


I am somewhat puzzled by how this array of CPerson objects was intiailized.
Why not use pNewPersons = new CPerson[nCount] instead?

In addition, is this part of the C++ standard?

Thanks for your help.

Adrian





---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Pulkkinen Esa <esap@cs.tut.fi>
Date: 1997/03/08
Raw View
[lib.new.delete.array] and [lib.new.delete.single]
have the following wording about
void* operator new[](std::size_t size, const std::nothrow_t&) throw() and
void* operator new  (std::size_t size, const std::nothrow_t&) throw()

"Effects:
    Same  as above, except that it is called by a placement version of a
    new-expression when a C++ program prefers a null pointer  result  as
    an error indication, instead of a bad_alloc exception."

What does the above mean exactly? Is this a typo, should it read
"non-placement version of the new expression" instead of "placement
version of the new expression"?.
--
   Esa Pulkkinen                        | C++ programmers do it virtually
   E-Mail:  esap@cs.tut.fi              | everywhere with class, resulting
   WWW   :  http://www.cs.tut.fi/~esap/ | in multiple inheritance.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: tony@online.tmx.com.au (Tony Cook)
Date: 1997/03/10
Raw View
Pulkkinen Esa (esap@cs.tut.fi) wrote:
: [lib.new.delete.array] and [lib.new.delete.single]
: have the following wording about
: void* operator new[](std::size_t size, const std::nothrow_t&) throw() and
: void* operator new  (std::size_t size, const std::nothrow_t&) throw()

: "Effects:
:     Same  as above, except that it is called by a placement version of a
:     new-expression when a C++ program prefers a null pointer  result  as
:     an error indication, instead of a bad_alloc exception."

: What does the above mean exactly? Is this a typo, should it read
: "non-placement version of the new expression" instead of "placement
: version of the new expression"?.

A placement version, as in:

  T*p = new (nothrow) T;

--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/03/10
Raw View
Pulkkinen Esa <esap@cs.tut.fi> writes:

|>  [lib.new.delete.array] and [lib.new.delete.single]
|>  have the following wording about
|>  void* operator new[](std::size_t size, const std::nothrow_t&) throw() and
|>  void* operator new  (std::size_t size, const std::nothrow_t&) throw()
|>
|>  "Effects:
|>      Same  as above, except that it is called by a placement version of a
|>      new-expression when a C++ program prefers a null pointer  result  as
|>      an error indication, instead of a bad_alloc exception."
|>
|>  What does the above mean exactly? Is this a typo, should it read
|>  "non-placement version of the new expression" instead of "placement
|>  version of the new expression"?.

No.  It is a quirk of C++ terminology.  All new operators with more than
one argument are refered to, collectively, as placement new.
(Historically, the use of the extra arguments was to determine where the
allocated memory would be placed.)

--
James Kanze      home:     kanze@gabi-soft.fr        +33 (0)1 39 55 85 62
                 office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
     -- Conseils en informatique industrielle --
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]