<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<title>C++ Dynamic Arrays</title>
</head>
<body>
<h1>C++ Dynamic Arrays</h1>

<p>
ISO/IEC JTC1 SC22 WG21 N2648 = 08-0158 - 2008-05-16
</p>

<p>
Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org
<br>
Matt Austern, austern@google.com
</p>

<p>
<strong>NOTE:</strong>
This proposal is intended for consideration for
Technical Report 2.

<h2><a name="Problem">Problem</a></h2>

<p>
Programs can become more efficient
when they can bind aspects of their execution
earlier in program development.
As an example,
the <samp>std::unordered_map</samp> container
provides more functionality than <samp>std::vector</samp>,
but <samp>std::vector</samp>
provides better performance
when the programmer can bind indexes
to a dense, but extensible, range near zero.
Going further, built-in arrays
provide even better performance
by binding the range end at compilation time.
</p>

<p>
Unfortunately, for some applications,
the range end is known at container construction
but not at compilation time.
So, built-in arrays are not applicable.
On the other hand,
<samp>std::vector</samp> is more general than needed,
as it permits an extensibility that is not required.
Ideally, we would like to be able to specify a container
where the index end is bound at construction,
but does not change thereafter.
</p>

<p>
The C programming language has such a container
in the form of <dfn>variable-length arrays</dfn>.
They are not general in that
they are limited to automatic variables,
but given that restriction
they are nearly as efficient as normal arrays,
requiring only mark/release stack allocation
and maintenance of a frame pointer.
(Maintaining a frame pointer is a good idea anyway.)
Unfortunately the detailed type semantics of C variable-length arrays
are probably not acceptable to C++,
so we cannot simply adopt them.
</p>

<p>
The <samp>std::valarray</samp> container is intermediate
between built-in arrays and <samp>std::vector</samp>,
but as it supports a <samp>resize</samp> method,
it cannot hold its size fixed for the lifetime of the variable.
Furthermore, <samp>std::valarray</samp>
supports compound member assignment operators
that imply such operators in the parameter type.
Such implications are workable only for types with "full interfaces",
not for general types.
</p>

<h2><a name="Solution">Solution</a></h2>

<p>
Instead of adopting C variable-length arrays,
we propose to define a new facility for arrays
where the number of elements is bound at construction.
We call these dynamic arrays, <samp>dynarray</samp>.
In keeping with C++ practice,
we wish to make <samp>dynarray</samp>s
usable with more than just automatic variables.
But to take advantage of the efficiency stack allocation,
we wish to make <samp>dynarray</samp> optimizable
when used as an automatic variable.
</p>

<p>
Therefore, we propose to define <samp>dynarray</samp>
so that compilers can recognize and implement
construction and destruction directly,
without appeal to any particular standard library implementation.
However, to minimize the necessary burden on compilers,
we propose that <samp>dynarray</samp> can be implemented as a pure library,
although with lost optimization opportunity.
</p>

<p>
We believe that the compilers can introduce the optimization
without impact on source or binary compatiblity.
There may be some change in code profiles and operator new calls
as a result of that optimization,
but such risks are common to compiler and library upgrades.
</p>

<p>
Syntactically, our proposal follows the lead of
<samp>std::array</samp> and <samp>std::vector</samp> containers.
Semantically, our proposal follows the lead of built-in arrays.
That is,
we do not require more out of <samp>std::dynarray</samp> element types
than we do of standard array element types.
</p>

<p>
The <samp>dynarray</samp> constructor has a parameter
indicating the number of elements in the container.
<samp>Dynarray</samp> requires an element type with a default constructor,
just as the built-in array requires.
Note that <samp>dynarray</samp> does not provide a default constructor,
because there is no reasonable default size,
and hence the <samp>dynarray</samp>
may not take a <samp>dynarray</samp> as an element.
</p>

<p>
<samp>Dynarray</samp> provides a copy constructor,
but use of the copy constructor requires that the element type
also have a copy constructor.
The presence of this constructor implies that
users cannot explicitly instantiate the <samp>dynarray</samp> template class
on a type that does not have a copy constructor.
This practice already exists in the standard library.
</p>

<p>
<samp>Dynarray</samp> provides random access iterators,
likely implemented as pointers.
The elements must be contiguously allocated,
to enable access via pointer arithmetic.
</p>

<p>
<samp>Dynarray</samp> also provides reverse iterators,
but these definitions imply that
the compiler implementation depends on the standard library implementation,
which is the reverse of the normal dependence.
</p>

<h2><a name="Presentation">Presentation</a></h2>

<p>
Within the proposal,
regular code font indicates normative code
and variable code font indicates an example implementation.
The example implementation is a pure library implementation,
and does not include the stack allocation optimization.
Thus, the example implementation is a minimal conforming implementation.
</p>

<p>
Within the example,
regular code font indicates example code.
There is no use of variable code font.
</p>

<p>
Within both the proposal and the example,
sample font is part of the commentary
and not part of either the proposal or the example.
This font is usually visually indistinguishable from code font,
but should be clear from context.
</p>

<p>
The code for the definition, implementation and subsequent example
can be extracted from the HTML source
with the following <samp>sed</samp> script.
</p>

<pre><samp>
	1,/&lt;code&gt;/		d
	/&lt;\/code&gt;/,/&lt;code&gt;/	d
				s|&lt;var&gt;||g
				s|&lt;/var&gt;||g
				s|&amp;lt;|&lt;|g
				s|&amp;gt;|&gt;|g
				s|&amp;amp;|\&amp;|g
</samp></pre>

<p>
First,
to enable a fully compilable implementation and example,
we include appropriate library headers
then avoid reliance on a concept-enabled compiler,
and finally avoid reliance on other C++0x features.
</p>

<pre><code><var>
#include &lt;stddef.h&gt;
#include &lt;cstring&gt;
#include &lt;algorithm&gt;
#include &lt;stdexcept&gt;
#include &lt;iostream&gt;
#include &lt;memory&gt;
#define DefaultConstructible typename
#define CPP0X( ignore )
</var></code></pre>

<h2><a name="Proposal">Proposal</a></h2>

<p>
The <samp>dynarray</samp> container definition is as follows.
The section, paragraph, and table references
are based on those of
<cite>
<a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2588.pdf">
N2588</a> Working Draft, Standard for Programming Language C++</cite>,
Pete Becker, March 2008.
</p>

<h3>Chapter 23 Containers library [containers]</h3>

<p>
Add <samp>&lt;dynarray&gt;</samp> to table 87:
</p>

<blockquote>
<table>
<caption>Table 87: Containers library summary</caption>
<tbody>
<tr><th>Subclause</th><th>Header(s)</th></tr>
<tr><td valign=top>23.1 Requirements</td></tr>
<tr><td valign=top>23.2 Sequence containers</td>
<td><samp>&lt;array&gt;<br>
&lt;deque&gt;<br>
<ins>&lt;dynarray&gt;</ins><br>
&lt;list&gt;<br>
&lt;queue&gt;<br>
&lt;stack&gt;<br>
&lt;vector&gt;</samp></td></tr>
<tr><td valign=top>23.3 Associative containers</td>
<td><samp>&lt;map&gt;<br>
&lt;set&gt;</samp></td></tr>
<tr><td valign=top>23.3.5 <samp>bitset</samp></td>
<td><samp>&lt;bitset&gt;</samp></td></tr>
<tr><td valign=top>23.4 Unordered associative containers</td>
<td><samp>&lt;unordered_map&gt;<br>
&lt;unordered_set&gt;</samp></td></tr>
</tbody>
</table>
</blockquote>

<h3>23.1.1 Sequence containers [sequence.reqmts]</h3>

<p>
In table 93, Optional sequence container operations,
add <samp>dynarray</samp> to the list of containers
for operations
<samp>front</samp>,
<samp>back</samp>,
<samp>a[n]</samp>, and
<samp>at(n)</samp>.
</p>

<p>
<em>[Note to the Editor:
The similar edits for <samp>array</samp>
appear to be missing.
This column seems redundant with the synopsis.]</em>
</p>

<h3>23.2 Sequence containers [sequences]</h3>

<p>
Add a new synopsis:
</p>

<blockquote>
<p>
<b>Header <samp>&lt;dynarray&gt;</samp> synopsis</b>
</p>

<pre><code>
namespace std {
template&lt; DefaultConstructible T &gt;
struct dynarray;
} // namespace std
</code></pre>
</blockquote>

<h3>23.2.7 Class template <samp>dynarray</samp> [dynarray]</h3>

<p>
Add a new section:
</p>

<blockquote>

<p>
The header <samp>&lt;dynarray&gt;</samp>
defines a class template for storing sequences of objects
where the size is fixed at construction.
A <samp>dynarray</samp> supports random access iterators.
An instance of <samp>dynarray&lt;T&gt;</samp>
stores elements of type <samp>T</samp>.
The elements of a <samp>dynarray</samp> are stored contiguously,
meaning that if <samp>d</samp> is an <samp>dynarray&lt;T&gt;</samp>
then it obeys the identity
<samp>&amp;a[n] == &amp;a[0] + n</samp> for all <samp>0 &lt;= n &lt; N</samp>.
</p>

<p>
Unless otherwise specified,
all array operations are as described in 23.1.
Descriptions are provided here
only for operations on <samp>dynarray</samp>
that are not described in that clause
or for operations where there is additional semantic information.
</p>

<p>
All operations except construction and destruction
shall have constant-time complexity.
</p>

<pre><code>
namespace std {
template&lt; DefaultConstructible T &gt;
struct dynarray
{
    // types:
    typedef       T                               value_type;
    typedef       T&amp;                              reference;
    typedef const T&amp;                              const_reference;
    typedef       <var>T*</var>                              iterator;
    typedef <var>const T*</var>                              const_iterator;
    typedef std::reverse_iterator&lt;iterator&gt;       reverse_iterator;
    typedef std::reverse_iterator&lt;const_iterator&gt; const_reverse_iterator;
    typedef <var>size_t</var>                                size_type;
    typedef <var>ptrdiff_t</var>                             difference_type;

    // fields:
<var>private:</var>
    <var>T*        store;</var>
    <var>size_type count;</var>

    // helper functions:
    <var>void check(size_type n)
        { if ( n &gt;= count ) throw out_of_range("dynarray"); }</var>
    <var>T* alloc(size_type n)
        { return reinterpret_cast&lt;T*&gt;( new char[ n*sizeof(T) ] ); }</var>

public:
    // construct and destruct:
    dynarray() <var>CPP0X(</var> = delete <var>)</var> ;
    const dynarray operator=(const dynarray&amp;) <var>CPP0X(</var> = delete <var>)</var> ;

    explicit dynarray(size_type <var>c</var>)
        <var>: store( alloc( c ) ), count( c )
        { size_type i;
          try {
              for ( size_type i = 0; i &lt; count; ++i )
                  new (store+i) T;
          } catch ( ... ) {
              for ( ; i > 0; --i )
                 (store+(i-1))->~T();
              throw;
          } }</var>

    dynarray(const dynarray&amp; <var>d</var>)
        <var>: store( alloc( d.count ) ), count( d.count )
        { try { uninitialized_copy( d.begin(), d.end(), begin() ); }
          catch ( ... ) { delete store; throw; } }</var>

    ~dynarray()
        <var>{ for ( size_type i = 0; i &lt; count; ++i )
              (store+i)->~T();
          delete[] store; }</var>

    // iterators:
    iterator       begin()        <var>{ return store; }</var>
    const_iterator begin()  const <var>{ return store; }</var>
    const_iterator cbegin() const <var>{ return store; }</var>
    iterator       end()          <var>{ return store + count; }</var>
    const_iterator end()    const <var>{ return store + count; }</var>
    const_iterator cend()   const <var>{ return store + count; }</var>

    reverse_iterator       rbegin()       
        <var>{ return reverse_iterator(end()); }</var>
    const_reverse_iterator rbegin()  const
        <var>{ return reverse_iterator(end()); }</var>
    reverse_iterator       rend()         
        <var>{ return reverse_iterator(begin()); }</var>
    const_reverse_iterator rend()    const
        <var>{ return reverse_iterator(begin()); }</var>

    // capacity:
    size_type size()     const <var>{ return count; }</var>
    size_type max_size() const <var>{ return count; }</var>
    bool      empty()    const <var>{ return false; }</var>

    // element access:
    reference       operator[](size_type <var>n</var>)       <var>{ return store[n]; }</var>
    const_reference operator[](size_type <var>n</var>) const <var>{ return store[n]; }</var>

    reference       front()       <var>{ return store[0]; }</var>
    const_reference front() const <var>{ return store[0]; }</var>
    reference       back()        <var>{ return store[count-1]; }</var>
    const_reference back()  const <var>{ return store[count-1]; }</var>

    const_reference at(size_type <var>n</var>) const <var>{ check(n); return store[n]; }</var>
    reference       at(size_type <var>n</var>)       <var>{ check(n); return store[n]; }</var>

    // data access:
    T*       data()       <var>{ return store; }</var>
    const T* data() const <var>{ return store; }</var>
};

} // namespace std
</code></pre>

</blockquote>

<h3>23.2.7.1 <samp>dynarray</samp> constructor and destructor [dynarray.cons]</h3>

<p>
Add a new section:
</p>

<blockquote>

<p>
<samp>dynarray(size_type <var>c</var>);</samp>
</p>

<blockquote>
<p>
<i>Requires:</i>
The constructor parameter shall be greater than zero.
</p>
</blockquote>

<blockquote>
<p>
<i>Effects:</i>
May or may not invoke the global <samp>operator new</samp>.
</p>
</blockquote>

<p>
<samp>dynarray(const dynarray&amp; <var>d</var>);</samp>
</p>

<blockquote>
<p>
<i>Requires:</i>
T is Copy Constructible.
</p>
</blockquote>

<blockquote>
<p>
<i>Effects:</i>
May or may not invoke the global <samp>operator new</samp>.
</p>
</blockquote>

<p>
<samp>~dynarray();</samp>
</p>

<blockquote>
<p>
<i>Effects:</i>
Invokes the global <samp>operator delete</samp>
if and only if
the constructor invoked the global <samp>operator new</samp>.
</p>
</blockquote>

</blockquote>

<h3>23.2.7.2 <samp>dynarray::size</samp> [dynarray.size]</h3>

<p>
Add a new section:
</p>

<blockquote>

<p>
<samp>size_type size() const;</samp>
</p>

<blockquote>
<p>
<i>Returns:</i>
Returns the argument to the constructor of the object.
</p>
</blockquote>

</blockquote>

<h3>23.2.7.3 <samp>dynarray::data</samp> [dynarray.data]</h3>

<p>
Add a new section:
</p>

<blockquote>

<p>
<samp>T* data();</samp><br>
<samp>T* data() const;</samp>
</p>

<blockquote>
<p>
<i>Returns:</i>
A pointer to the contiguous storage containing the elements.
</p>
</blockquote>

</blockquote>

<h2><a name="Example">Example</a></h2>

<p>
Finally, we show a simple set of uses of the container.
</p>

<p>
Declaring a reference parameter.
Using a const iterator.
</p>

<pre><code>
void dump( const std::dynarray&lt; int &gt; &amp; source )
{
    std::dynarray&lt; int &gt;::const_iterator src = source.begin();
    for ( ; src != source.end(); src++ )
        std::cout << " " << *src;
    std::cout << std::endl;
}
</code></pre>

<p>
Declaring a local dynarray of computed size. 
Using 
<samp>front</samp>,
<samp>back</samp>,
and a non-const iterator.
</p>

<pre><code>
void lowrap(       std::dynarray&lt; int &gt; &amp; target,
             const std::dynarray&lt; int &gt; &amp; source )
{
    dump( source );

    std::dynarray&lt; int &gt; sorted( source );
    dump( sorted );

    std::sort( sorted.begin(), sorted.end() );
    dump( sorted );

    const int* srt = &amp;sorted.front();
    std::dynarray&lt; int &gt;::iterator tgt( target.begin() );
    for ( ; tgt != target.end(); tgt++ ) {
        *tgt = *srt;
        if ( srt == &amp;sorted.back() )
            srt = &amp;sorted.front();
        else
            srt++;
    }
    dump( target );
}
</code></pre>

<p>
Declaring a local dynarray of fixed size.
Using
<samp>size</samp>,
<samp>operator[]</samp>,
<samp>at</samp>,
index iteration,
and pointer iteration.
</p>

<pre><code>
int main() {
    std::dynarray&lt; int &gt; alpha(8);
    std::dynarray&lt; int &gt; gamma(3);
    for ( std::dynarray&lt; int &gt;::size_type i = 0; i < gamma.size(); i++ )
	gamma[i] = 4 - i;
    lowrap( alpha, gamma );
    int sum = 0;
    for ( std::dynarray&lt; int &gt;::size_type i = 0; i < alpha.size(); i++ )
	sum += alpha.at(i);
    return sum;
}
</code></pre>

</body>
</html>
