# Class scoped_array

Class scoped_array stores a pointer to a dynamically allocated array. (Dynamically allocated arrays are allocated with the C++ `new[]` expression.) The array pointed to is guaranteed to be deleted, either on destruction of the scoped_array, or via an explicit scoped_array::reset().

Class scoped_array is a simple solution for simple needs. It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics. Both its name and enforcement of semantics (by being noncopyable) signal its intent to retain ownership solely within the current scope. By being noncopyable, it is safer than shared_array for pointers which should not be copied.

Because scoped_array is so simple, in its usual implementation every operation is as fast as a built-in array pointer and it has no more space overhead that a built-in array pointer.

It cannot be used in C++ Standard Library containers. See shared_array if scoped_array does not meet your needs.

Class scoped_array cannot correctly hold a pointer to a single object. See scoped_ptr for that usage.

A C++ Standard Library vector is a heavier duty alternative to a scoped_array.

The class is a template parameterized on `T`, the type of the object pointed to. `T` must meet the smart pointer common requirements.

## Class scoped_array Synopsis

    #include <boost/smart_ptr.hpp>
    namespace boost {

    template<typename T> class scoped_array : noncopyable {

     public:
       typedef T element_type;

       explicit scoped_array( T* p=0 );  // never throws
       ~scoped_array();

       void reset( T* p=0 );

       T& operator[](std::size_t i) const;  // never throws
       T* get() const;  // never throws
       };
    }

## Class scoped_array Members

### scoped_array element_type

    typedef T element_type;

Provides the type of the stored pointer.

### scoped_array constructors

    explicit scoped_array( T* p=0 );  // never throws

Constructs a `scoped_array`, storing a copy of `p`, which must have been allocated via a C++ `new`[] expression or be 0.

T is not required be a complete type. See Common Requirements.

### scoped_array destructor

    ~scoped_array();

Deletes the array pointed to by the stored pointer. Note that in C++ `delete`[] on a pointer with a value of 0 is harmless.

Does not throw exceptions.

### scoped_array reset

    void reset( T* p=0 )();

If p is not equal to the stored pointer, deletes the array pointed to by the stored pointer and then stores a copy of p, which must have been allocated via a C++ `new[]` expression or be 0.

Does not throw exceptions.

### scoped_array operator[]

`T& operator[](std::size_t i) const; // never throws`

Returns a reference to element `i` of the array pointed to by the stored pointer.

Behavior is undefined (and almost certainly undesirable) if `get()==0`, or if `i` is less than 0 or is greater or equal to the number of elements in the array.

### scoped_array get

    T* get() const;  // never throws

T is not required be a complete type. See Common Requirements.

Returns the stored pointer.

## Class scoped_array example

[To be supplied. In the meantime, see smart_ptr_test.cpp.]

Revised 24 May, 2001

© Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.