Topic: Operator new?
Author: "Kjetil Kristoffer Solberg" <kjetilso@tihlde.org>
Date: 1999/10/01 Raw View
Hi,
I was wondering if the C++ new operator was implemented using malloc? If
so, why use new?
regards
-- Kjetil K. Solberg
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/10/01 Raw View
Kjetil Kristoffer Solberg wrote:
>
> Hi,
> I was wondering if the C++ new operator was implemented using malloc? If
> so, why use new?
Depends on the implementation. It doesn't have to.
Even if it did new has the advanage of being type safe and also performing
initializaiton of the object.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/02 Raw View
In article <7t2fgu$nr7$1@oslo-nntp.eunet.no>, Kjetil Kristoffer Solberg
<kjetilso@tihlde.org> writes
>
>Hi,
> I was wondering if the C++ new operator was implemented using malloc? If
>so, why use new?
because operator new is only one half of what happens when you use a new
expression. The other half is calling the appropriate ctor. In
addition the programmer can provide other (overloads) or replacement
(often used for debugging, and GC) versions of operator new.
Please note that writing about this is complicated because 'operator
new' is not all that happens when you write 'new Mytype' That expression
calls the relevant operator new (possibly user provided) and then calls
a ctor (possibly user provided).
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/10/02 Raw View
Kjetil Kristoffer Solberg wrote:
>
> Hi,
> I was wondering if the C++ new operator was implemented using malloc? If
> so, why use new?
The 'new' operator calls 'operator new()' to allocate memory, and then
invokes the appropriate constructor in that allocated memory. 'operator
new()' can be implemented using malloc(), but there are two key
advantages to using the 'new' operator over calling malloc():
1. You can override 'operator new()', both globally and on a per-class
basis, changing the way memory is allocated.
2. It's slightly simpler and easier and, most imporatantly, safer, to
write:
A* pA = new A(constructor_argument);
than
A* pA = (A*)std::malloc(sizeof(A));
if(pA==(A*)NULL)
throw(std::bad_alloc);
A::A(pA, constructor_argument);
A::A() is a currently illegal construct that I'm using as an example of
what would have to be done differently if new didn't exist. It's
equivalent the using placement new in legal C++:
new(pA) A(constructor_argument);
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]