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:
<snip>
> 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
changing.

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.

Ben.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net