📄 memory.html
字号:
<HTML><HEAD><TITLE><memory></TITLE></HEAD><BODY><H1><A NAME="<memory>"><CODE><memory></CODE></A></H1><HR><P>Include the <A HREF="index.html#STL">STL</A>standard header <B><CODE><memory></CODE></B>to define a class, an operator, and several templates that helpallocate and free objects.</P><PRE>namespace std {template<class Ty> class <B><A HREF="#allocator">allocator</A></B>;template<> class <B><A HREF="#allocator<void>">allocator<void></A></B>;template<class FwdIt, class Ty> class <B><A HREF="#raw_storage_iterator">raw_storage_iterator</A></B>;template<class Ty> class <B><A HREF="#auto_ptr">auto_ptr</A></B>;template<class Ty> class <B><A HREF="#auto_ptr_ref">auto_ptr_ref</A></B>; // TEMPLATE OPERATORStemplate<class Ty> bool <B><A HREF="#operator==">operator==</A></B>(allocator<Ty>& left, allocator<Ty>& right);template<class Ty> bool <B><A HREF="#operator!=">operator!=</A></B>(allocator<Ty>& left, allocator<Ty>& right); // TEMPLATE FUNCTIONStemplate<class Ty> pair<Ty *, ptrdiff_t> <B><A HREF="#get_temporary_buffer">get_temporary_buffer</A></B>(ptrdiff_t count);template<class Ty> void <B><A HREF="#return_temporary_buffer">return_temporary_buffer</A></B>(Ty *pbuf);template<class InIt, class FwdIt> FwdIt <B><A HREF="#uninitialized_copy">uninitialized_copy</A></B>(InIt first, InIt last, FwdIt dest);template<class FwdIt, class Ty> void <B><A HREF="#uninitialized_fill">uninitialized_fill</A></B>(FwdIt first, FwdIt last, const Ty& val);template<class FwdIt, class Size, class Ty> void <B><A HREF="#uninitialized_fill_n">uninitialized_fill_n</A></B>(FwdIt first, Size count, const Ty& val); };</PRE><H2><A NAME="allocator"><CODE>allocator</CODE></A></H2><PRE>template<class Ty> class <B>allocator</B> { typedef size_t <B><A HREF="#allocator::size_type">size_type</A></B>; typedef ptrdiff_t <B><A HREF="#allocator::difference_type">difference_type</A></B>; typedef Ty *<B><A HREF="#allocator::pointer">pointer</A></B>; typedef const Ty *<B><A HREF="#allocator::const_pointer">const_pointer</A></B>; typedef Ty& <B><A HREF="#allocator::reference">reference</A></B>; typedef const Ty& <B><A HREF="#allocator::const_reference">const_reference</A></B>; typedef Ty <B><A HREF="#allocator::value_type">value_type</A></B>; pointer <B><A HREF="#allocator::address">address</A></B>(reference val) const; const_pointer <B><A HREF="#allocator::address">address</A></B>(const_reference val) const; template<class Other> struct <B><A HREF="#allocator::rebind">rebind</A></B>; <B><A HREF="#allocator::allocator">allocator</A></B>() throw(); template<class Other> <B><A HREF="#allocator::allocator">allocator</A></B>(const allocator<Other>& right) throw(); template<class Other> allocator& <B><A HREF="#allocator::operator=">operator=</A></B>(const allocator<Other>& right); pointer <B><A HREF="#allocator::allocate">allocate</A></B>(size_type count, allocator<void>::const_pointer Other *hint = 0); void <B><A HREF="#allocator::deallocate">deallocate</A></B>(pointer ptr, size_type count); void <B><A HREF="#allocator::construct">construct</A></B>(pointer ptr, const Ty& val); void <B><A HREF="#allocator::destroy">destroy</A></B>(pointer ptr); size_type <B><A HREF="#allocator::max_size">max_size</A></B>() const throw(); };</PRE><P>The template class describes an object that managesstorage allocation and freeing for arrays of objects of type <CODE>Ty</CODE>.An object of class <CODE>allocator</CODE> is the default<B><A NAME="allocator object">allocator object</A></B>specified in the constructors for severalcontainer template classes in the Standard C++ library.</P><P>Template class <CODE>allocator</CODE> supplies severaltype definitions that are rather pedestrian.They hardly seem worth defining.But another class with the same membersmight choose more interesting alternatives.Constructing a container with an allocator object of such a classgives individual control over allocation and freeingof elements controlled by that container.</P><P>For example, an allocator object might allocate storage on a<B><A NAME="private heap">private heap</A></B>.Or it might allocate storage on a<B><A NAME="far heap">far heap</A></B>, requiring nonstandardpointers to access the allocated objects. Or it might specify,through the type definitions it supplies, that elements beaccessed through special<B><A NAME="accessor objects">accessor objects</A></B> that manage<B><A NAME="shared memory">shared memory</A></B>, or perform automatic<B><A NAME="garbage collection">garbage collection</A></B>.Hence, a class that allocates storage using an allocator objectshould use these types religiously for declaring pointer andreference objects (as do the containers in the Standard C++ library).</P><P>Thus, an allocator defines the types (among others):</P><UL><LI><A HREF="#allocator::pointer"><CODE>pointer</CODE></A>-- behaves like a pointer to <CODE>Ty</CODE><LI><A HREF="#allocator::const_pointer"><CODE>const_pointer</CODE></A>-- behaves like aconst pointer to <CODE>Ty</CODE><LI><A HREF="#allocator::reference"><CODE>reference</CODE></A>-- behaves like a reference to <CODE>Ty</CODE><LI><A HREF="#allocator::const_reference"><CODE>const_reference</CODE></A>-- behaves like aconst reference to <CODE>Ty</CODE></UL><P>These types specify the form that pointers and referencesmust take for allocated elements.(<CODE>allocator::pointer</CODE> is not necessarilythe same as <CODE>Ty *</CODE> for all allocator objects, even thoughit has this obvious definition for class <CODE>allocator</CODE>.)</P><H3><A NAME="allocator::address"><CODE>allocator::address</CODE></A></H3><PRE>pointer <B><A HREF="#allocator::address">address</A></B>(reference val) const;const_pointer <B><A HREF="#allocator::address">address</A></B>(const_reference val) const;</PRE><P>The member functions return the address of <CODE>val</CODE>,in the form that pointers must take for allocated elements.</P><H3><A NAME="allocator::allocate"><CODE>allocator::allocate</CODE></A></H3><PRE>pointer <B>allocate</B>(size_type count, allocator<void>::const_pointer *hint = 0);</PRE><P>The member function allocates storage foran array of <CODE>count</CODE> elements of type <CODE>Ty</CODE>, by calling<CODE>operator new(count)</CODE>.It returns a pointer to the allocated object.The <CODE>hint</CODE> argument helps some allocatorsin improving locality of reference -- a valid choiceis the address of an object earlier allocated by the same allocatorobject, and not yet deallocated. To supply nohint, use a null pointer argument instead.</P><H3><A NAME="allocator::allocator"><CODE>allocator::allocator</CODE></A></H3><PRE><B>allocator</B>() throw();template<class Other> <B>allocator</B>(const allocator<Other>& right) throw();</PRE><P>The constructor does nothing. In general, however, an allocator objectconstructed from another allocator object should compare equal to it(and hence permit intermixing of object allocation and freeing betweenthe two allocator objects).</P><H3><A NAME="allocator::const_pointer"><CODE>allocator::const_pointer</CODE></A></H3><PRE>typedef const Ty *<B>pointer</B>;</PRE><P>The pointer type describes an object <CODE>ptr</CODE> that candesignate, via the expression <CODE>*ptr</CODE>, any const objectthat an object of template class<CODE>allocator</CODE> can allocate.</P><H3><A NAME="allocator::const_reference"><CODE>allocator::const_reference</CODE></A></H3><PRE>typedef const Ty& <B>const_reference</B>;</PRE><P>The reference type describes an object that candesignate any const object that an object of template class<CODE>allocator</CODE> can allocate.</P><H3><A NAME="allocator::construct"><CODE>allocator::construct</CODE></A></H3><PRE>void <B>construct</B>(pointer ptr, const Ty& val);</PRE><P>The member function constructs an object of type <CODE>Ty</CODE>at <CODE>ptr</CODE> by evaluating the placement<CODE>new</CODE> expression <CODE>new ((void *)ptr) Ty(val)</CODE>.</P><H3><A NAME="allocator::deallocate"><CODE>allocator::deallocate</CODE></A></H3><PRE>void <B>deallocate</B>(pointer ptr, size_type count);</PRE><P>The member function frees storage forthe array of <CODE>count</CODE> objects of type<CODE>Ty</CODE> beginning at <CODE>ptr</CODE>, by calling<CODE>operator delete(ptr)</CODE>.The pointer <CODE>ptr</CODE> must have been earlier returned by a call to<CODE><A HREF="#allocator::allocate">allocate</A></CODE> for an allocatorobject that compares equal to <CODE>*this</CODE>, allocating an array objectof the same size and type.<CODE>deallocate</CODE> never throws an exception.</P><H3><A NAME="allocator::destroy"><CODE>allocator::destroy</CODE></A></H3><PRE>void <B>destroy</B>(pointer ptr);</PRE><P>The member function destroys the objectdesignated by <CODE>ptr</CODE>,by calling the destructor <CODE>ptr->Ty::~Ty()</CODE>.</P><H3><A NAME="allocator::difference_type"><CODE>allocator::difference_type</CODE></A></H3><PRE>typedef ptrdiff_t <B>difference_type</B>;</PRE><P>The signed integer type describes an object that can represent thedifference between the addresses of any two elements in a sequencethat an object of template class <CODE>allocator</CODE> can allocate.</P><H3><A NAME="allocator::max_size"><CODE>allocator::max_size</CODE></A></H3><PRE>size_type <B>max_size</B>() const throw();</PRE><P>The member function returns the length of the longest sequenceof elements of type <CODE>Ty</CODE> that an object of class<CODE>allocator</CODE> <I>might</I> be able to allocate.</P><H3><A NAME="allocator::operator="><CODE>allocator::operator=</CODE></A></H3><PRE>template<class Other> allocator& <B>operator=</B>(const allocator<Other>& right);</PRE><P>The template assignment operator does nothing.In general, however, an allocator objectassigned to another allocator object should compare equal to it(and hence permit intermixing of object allocation and freeing betweenthe two allocator objects).</P><H3><A NAME="allocator::pointer"><CODE>allocator::pointer</CODE></A></H3><PRE>typedef Ty *<B>pointer</B>;</PRE><P>The pointer type describes an object <CODE>ptr</CODE> that candesignate, via the expression <CODE>*ptr</CODE>, any objectthat an object of template class<CODE>allocator</CODE> can allocate.</P><H3><A NAME="allocator::rebind"><CODE>allocator::rebind</CODE></A></H3><PRE>template<class Other> struct <B>rebind</B> { typedef allocator<Other> <B>other</B>; };</PRE><P>The member template class defines the type<B><CODE><A NAME="allocator::other">other</A></CODE></B>.Its sole purpose is to provide the type name <CODE>allocator<Other></CODE>given the type name <CODE>allocator<Ty></CODE>.</P><P>For example, given an allocator object <CODE>al</CODE> of type<CODE>A</CODE>, you can allocate an object of type<CODE>Other</CODE> with the expression:</P><PRE>A::rebind<Other>::other(al).allocate(1, (Other *)0)</PRE><P>Or, you can simply name its pointer type by writing the type:</P><PRE>A::rebind<Other>::other::pointer</PRE><H3><A NAME="allocator::reference"><CODE>allocator::reference</CODE></A></H3><PRE>typedef Ty& <B>reference</B>;</PRE><P>The reference type describes an object that candesignate any object that an object of template class<CODE>allocator</CODE> can allocate.</P><H3><A NAME="allocator::size_type"><CODE>allocator::size_type</CODE></A></H3><PRE>typedef size_t <B>size_type</B>;</PRE><P>The unsigned integer type describes an object that can representthe length of any sequence that an object of template class<CODE>allocator</CODE> can allocate.</P><H3><A NAME="allocator::value_type"><CODE>allocator::value_type</CODE></A></H3><PRE>typedef Ty <B>value_type</B>;</PRE><P>The type is a synonym for the template parameter <CODE>Ty</CODE>.</P><H2><A NAME="allocator<void>"><CODE>allocator<void></CODE></A></H2><PRE>template<> class <B>allocator<void></B> { typedef void *<B>pointer</B>; typedef const void *<B>const_pointer</B>; typedef void <B>value_type</B>; template<class Other> struct <B>rebind</B>; <B>allocator</B>() throw(); template<class Other> <B>allocator</B>(const allocator<Other>) throw(); template<class Other> allocator<void>& <B>operator=</B>(const allocator<Other>); };</PRE><P>The class explicitly specializes template class<A HREF="#allocator">allocator</A> for type <I>void.</I>Its constructors and assignment operator behave the same as for thetemplate class, but it defines only the types<A HREF="#allocator::const_pointer"><CODE>const_pointer</CODE></A>,<A HREF="#allocator::pointer"><CODE>pointer</CODE></A>,<A HREF="#allocator::value_type"><CODE>value_type</CODE></A>,and the nested template class<A HREF="#allocator::rebind"><CODE>rebind</CODE></A>.</P><H2><A NAME="auto_ptr"><CODE>auto_ptr</CODE></A></H2><PRE>template<class Ty> class <B>auto_ptr</B> {public: typedef Ty <B><A HREF="#auto_ptr::element_type">element_type</A></B>; explicit <B><A HREF="#auto_ptr::auto_ptr">auto_ptr</A></B>(Ty *ptr = 0) throw(); <B><A HREF="#auto_ptr::auto_ptr">auto_ptr</A></B>(auto_ptr<Ty>& right) throw(); template<class Other> <B><A HREF="#auto_ptr::auto_ptr">auto_ptr</A></B>(auto_ptr<Other>& right) throw(); <B><A HREF="#auto_ptr::auto_ptr">auto_ptr</A></B>(auto_ptr_ref<Ty> right) throw(); <B><A HREF="#auto_ptr::~auto_ptr">~auto_ptr</A></B>(); template<class Other> <B><A HREF="#auto_ptr::operator auto_ptr<Other>">operator auto_ptr<Other></A></B>() throw(); template<class Other> <B><A HREF="#auto_ptr::operator auto_ptr_ref<Other>">operator auto_ptr_ref<Other></A></B>() throw(); template<class Other> auto_ptr<Ty>& <B><A HREF="#auto_ptr::operator=">operator=</A></B>(auto_ptr<Other>& right) throw(); auto_ptr<Ty>& <B><A HREF="#auto_ptr::operator=">operator=</A></B>(auto_ptr<Ty>& right) throw(); Ty& <B><A HREF="#auto_ptr::operator*">operator*</A></B>() const throw(); Ty *<B><A HREF="#auto_ptr::operator->">operator-></A></B>() const throw(); Ty *<B><A HREF="#auto_ptr::get">get</A></B>() const throw(); Ty *<B><A HREF="#auto_ptr::release">release</A></B>() const throw(); void <B><A HREF="#auto_ptr::reset">reset</A></B>(Ty *ptr = 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -