Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2003-11-11 08:20:11

Dave <better_cs_now_at_[hidden]> wrote:
> I do have one question about the array library. In the
> documentation, the statement is made that the advantage array has
> over vector is the fact that since vectors provide the semantics
> of dynamic arrays, "This results in some overhead in case only
> arrays with static sizes are needed.". When discussing this with
> my peers, I'd like to present something a little more concrete to
> them.
> If I have a number of elements that is known at compile time, why,
> *specifically*, should I prefer boost::array to std::vector?

The array is stored inside the class instance rather than separately
allocated from the heap. There is no need for additional pointer and
size member data. This saves time and memory.

Many member functions are trivial because of the fixed size. This
also saves time.

The array size is exactly what you specify, whereas std::vector may
round up the size of its internal array to allow for some growth.
This saves memory.

There may also be a reduction in mental overhead because you will
never have to think about the possibility of the array size

One disadvantage is that you can catch a failed heap allocation
whereas a failed automatic allocation is normally fatal. Allocating
large arrays in automatic storage remains a bad idea.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at