📄 allocate.htm
字号:
<html>
<head>
<title>allocator</title>
<head>
<body bgcolor="#FFFFFF">
<a name="here"></a>
<img src="allban.gif">
<pre>
<font size=5>Class Name</font> allocator
<font size=5>Header File</font> <memory>
<font size=5>Classification</font> Memory Management
</pre>
<a href="allocate.htm#class-descrip">Class Description</a>
<h1>Member Classes</h1>
<p>
None
</p>
<h1>Methods</h1>
<pre>
<a href="allocate.htm#address1">pointer address(reference x) const;</a>
<a href="allocate.htm#address2">const_pointer address(const_reference x) const;</a>
<a href="allocate.htm#allocate">pointer allocate(size_type n, typename allocator<void>::const_pointer hint = 0);</a>
<a href="allocate.htm#allocator1">allocator() throw();</a>
<a href="allocate.htm#allocator2">allocator(const allocator&) throw();</a>
<a href="allocate.htm#allocator3">template <class U> allocator(const allocator<U>& a) throw();</a>
<a href="allocate.htm#~allocator">~allocator() throw();</a>
<a href="allocate.htm#construct">void construct(pointer p, const T& val);</a>
<a href="allocate.htm#deallocate">void deallocate(pointer p, size_type n);</a>
<a href="allocate.htm#destroy">void destroy(pointer p);</a>
<a href="allocate.htm#max-size">size_type max_size() const throw();</a>
</pre>
<a href="allocate.htm#example"><h2>Example</h2></a>
<hr>
<a name="class-descrip"><h3>Class Description</h3></a>
<p>
The allocator class encapsulates information about the memory model being
used by the program. It encapsulates information about pointers and const
pointers, references and const references, size of objects, and the difference
between pointers allocation and deallocation functions. The information
contained in the allocator class is used by any function that performs
operation concerning the memory model. A function that inserts, extracts,
advances pointers that point to objects in a container needs information from
the allocator object in order to determine the size of the object, pointer
or reference.
</p>
<hr>
<pre>
<a name="address1">
Method address()</a>
Access Public
Classification Accessor
Syntax pointer address(reference x) const;
Parameters <em>x</em> is a reference to an object.
Returns This method returns the address of object x.
</pre>
<h3>Description</h3>
<p>
The address() method returns the pointer to object <em>x</em>. When passed a
reference to an object, address() will return a pointer to that object.
The pointer can then be dereferenced returning the actual object.
</p>
<hr>
<pre>
<a name="address2">
Method address()</a>
Access Public
Classification Accessor
Syntax const_pointer address(const_reference x) const;
Parameters <em>x</em> is a const reference to an object.
Returns This method returns the address of object x.
</pre>
<h3>Description</h3>
<p>
The address() method returns the const pointer to object <em>x</em>.
</p>
<hr>
<pre>
<a name="allocator1">
Method allocator()</a>
Access Public
Classification Constructor
Syntax allocator() throw();
Parameters None
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an allocator object.
</p>
<hr>
<pre>
<a name="allocator2">
Method allocator()</a>
Access Public
Classification Constructor
Syntax allocator(const allocator& a) throw();
Parameters <em>a</em> is a reference to the allocator object in which
the newly constructed object is a copy.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an allocator object which is a copy of allocator
object <em>a</em>.
</p>
<hr>
<pre>
<a name="allocator3">
Method allocator()</a>
Access Public
Classification Constructor
Syntax template <class U> allocator(const allocator<U>& a) throw();
Parameters <em>a</em> is a reference to the allocator object in which
the newly constructed object is a copy.
Returns None
</pre>
<h3>Description</h3>
<p>
This constructor constructs an allocator object which is a copy of allocator
object <em>a</em>.
</p>
<hr>
<pre>
<a name="allocate">
Method allocate()</a>
Access Public
Classification Modifier
Syntax pointer allocate(size_type n,
typename allocator<void>::const_pointer hint = 0);
Parameters <em>n</em> is the number of objects to allocate memory for.
<em>hint</em> is used to help to the allocator object for systems
where locality is an important factor when allocating
memory.
Returns None
</pre>
<h3>Description</h3>
<p>
The allocate() method allocates memory for <em>n</em> objects of size_type.
The memory is allocated but the objects are not constructed. The default
allocator uses new(size_t) to create the memory block. The <em>hint</em>
argument is implementation-dependent.
</p>
<hr>
<pre>
<a name="~allocator">
Method ~allocator()</a>
Access Public
Classification Destructor
Syntax ~allocator() throw();
Parameters None
Returns None
</pre>
<h3>Description</h3>
<p>
The destructor destroys an allocator object.
</p>
<hr>
<pre>
<a name="construct">
Method construct()</a>
Access Public
Classification Modifier
Syntax void construct(pointer p, const T& val);
Parameters <em>p</em> is a pointer returned by the allocate() method.
<em>val</em> is a reference to the object to be constructed.
Returns None
</pre>
<h3>Description</h3>
<p>
The construct() method is passed the pointer returned by allocate() then
constructs the object <em>val</em> by current its constructor.
</p>
<hr>
<pre>
<a name="deallocate">
Method deallocate()</a>
Access Public
Classification Modifier
Syntax void deallocate(pointer p, size_type n);
Parameters <em>p</em> points to the objects to deallocate.
<em>n</em> is the number of objects to deallocate
its storage.
Returns None
</pre>
<h3>Description</h3>
<p>
The deallocate() method deallocates all of the storage of <em>n</em> objects
of type size_type pointed to by the pointer <em>p</em>. The default
allocator uses the delete() to free the space. This space can now be used.
The actual objects should be destroyed before the deallocate() function is
called.
</p>
<hr>
<pre>
<a name="destroy">
Method destroy()</a>
Access Public
Classification Modifier
Syntax void destroy(pointer p);
Parameters <em>p</em> points to the object to be destroyed.
Returns None
</pre>
<h3>Description</h3>
<p>
The destroy() method destroys an object. It uses the pointer <em>p</em>
which is returned by the allocate() method. This method will explicitly
invoke T's destructor to destroy the object at <em>p</em>. The objects are
destroyed but the space that it occupied has not been deallocated.
</p>
<hr>
<pre>
<a name="max-size">
Method max_size()</a>
Access Public
Classification Accessor
Syntax size_type max_size() const throw();
Parameters None
Returns This method returns the maximum number of objects
that can be stored in an object using the current
memory model.
</pre>
<h3>Description</h3>
<p>
The max_size() method returns the maximum number of objects that can be
stored in an object using the current memory model.
</p>
<hr>
<a name="example"></a>
<img src="lego.gif">
<pre>
#include <memory>
#include <iostream>
void main(void)
{
allocator<char> Storage;
cout << Storage.max_size() << endl;
}
</pre>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -