blob: e3f35c061ff5d1428147820062001859297909d7 [file] [log] [blame]
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright
ownership. The ASF licenses this file to you under the Apache
License, Version 2.0 (the License); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
Copyright 1999-2007 Rogue Wave Software, Inc.
-->
<HTML>
<HEAD>
<TITLE>operator new</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="operatordelete.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="operators.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV>
<H2>operator new</H2>
<P><B>Library:</B>&nbsp;&nbsp;<A HREF="2-2.html">Language support</A></P>
<PRE><HR><B><I>Function</I></B><HR></PRE>
<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Global Functions</A></LI>
<LI><A HREF="#sec6">Global Operators</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
<H4>Non-Members</H4>
<UL><TABLE CELLPADDING=3>
<TR><TD VALIGN=top>
<A HREF="#idx1054">new()</A><BR>
</TD>
<TD VALIGN=top><A HREF="#idx1053">set_new_handler()</A><BR>
</TD>
<TD VALIGN=top></TD></TR>
</TABLE></UL>
<A NAME="sec2"><H3>Summary</H3></A>
<P>Storage allocation functions implicitly called by the corresponding new expressions to allocate storage suitably aligned to construct any object of a given size</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;new&gt;
namespace std {
class bad_alloc;
struct nothrow_t {};
extern const nothrow_t nothrow;
typedef void (*new_handler)();
new_handler set_new_handler new_handler) throw();
}
void* operator new(std::size_t) throw (std::bad_alloc);
void* operator new(std::size_t, const std::nothrow_t&amp;)
throw();
void* operator new[](std::size_t) throw(std::bad_alloc);
void* operator new[](std::size_t, const std::nothrow_t&amp;)
throw();
void* operator new(std::size_t, void*) throw();
void* operator new[](std::size_t, void*) throw();
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>The library provides definitions for six overloads of the global operator <SAMP>new</SAMP>. The functions are implicitly called as the first step during the evaluation of the corresponding <SAMP>new</SAMP> expression to allocate storage suitably aligned to construct one or more objects (for the array versions of the operator) of any type. The functions indicate failure by either throwing an exception object of type <B><I><A HREF="bad-alloc.html">bad_alloc</A></I></B> or by returning the null pointer. Replacements for the replaceable forms of the functions should always paired with the replacements for the corresponding overload of operator <SAMP>delete</SAMP>.</P>
<A NAME="sec5"><H3>Global Functions</H3></A>
<A NAME="idx1053"></A><PRE>new_handler
<B>set_new_handler</B>(new_handler handler) throw(); </PRE>
<UL>
<P>Establishes handler as the current <I>new_handler</I>. The first time <SAMP>set_new_handler()</SAMP> is called, it returns the null pointer. Subsequent calls to <SAMP>set_new_handler()</SAMP> return the previous value of <I>new_handler</I>.</P>
</UL>
<A NAME="sec6"><H3>Global Operators</H3></A>
<A NAME="idx1054"></A><PRE>void* operator <B>new</B>(std::size_t n) throw(std::bad_alloc); </PRE>
<UL>
<P>Allocation function implicitly called by a <SAMP>new</SAMP> expression to allocate <SAMP>n</SAMP> bytes of storage. The function attempts to allocate storage in a loop: if the allocation is successful, a pointer to the allocated memory is returned. Otherwise, if the last argument to <SAMP>std::set_new_handler()</SAMP> was not a null pointer, the function calls the current <I>new_handler</I>. Otherwise it throws an exception object of type <B><I><A HREF="bad-alloc.html">bad_alloc</A></I></B>. The loop terminates if the allocation is successful or the current <I>new_handler</I> doesn't return. A C++ program may replace this function.</P>
</UL>
<A NAME="idx1055"></A><PRE>void* operator <B>new</B>(std::size_t n, const std::nothrow_t&amp;) throw(); </PRE>
<UL>
<P>Allocation function similar to the one above, except that it indicates failure by returning a null pointer. A C++ program may replace this function.</P>
</UL>
<A NAME="idx1056"></A><PRE>void* operator <B>new</B>[](std::size_t n) throw(std::bad_alloc); </PRE>
<UL>
<P>Allocation function implicitly called by the array form of a <SAMP>new</SAMP> expression to allocate <SAMP>n</SAMP> bytes of storage. The function returns <SAMP>::operator new (n)</SAMP>. A C++ program may replace this function.</P>
</UL>
<A NAME="idx1057"></A><PRE>void* operator <B>new</B>[](std::size_t, const std::nothrow_t&amp;) throw(); </PRE>
<UL>
<P>Allocation function similar to the one above, except that it indicates failure by returning a null pointer. A C++ program may replace this function.</P>
</UL>
<A NAME="idx1058"></A><PRE>void* operator <B>new</B>(std::size_t, void *p) throw(); </PRE>
<UL>
<P>Allocation function implicitly called by the placement form of a <SAMP>new</SAMP> expression. Returns <SAMP>p</SAMP>. A C++ program may not replace this function.</P>
</UL>
<A NAME="idx1059"></A><PRE>void* operator <B>new</B>[](std::size_t, void *p) throw(); </PRE>
<UL>
<P>Allocation function implicitly called by the placement array form of a <SAMP>new</SAMP> expression. Returns <SAMP>p</SAMP>. A C++ program may not replace this function.</P>
</UL>
<A NAME="sec7"><H3>See Also</H3></A>
<P><B><I><A HREF="new-h.html">&lt;new&gt;</A></I></B>, <B><I><A HREF="bad-alloc.html">bad_alloc</A></I></B>, <B><I><A HREF="operatordelete.html">operator delete</A></I></B></P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 18.4</I></P>
<BR>
<HR>
<A HREF="operatordelete.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="operators.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>