# 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 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.

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

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

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.

### 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

Returns the stored pointer.

## Class scoped_array example

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

Revised December 8, 1999

© 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.