advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 2.7/5 | Rate this item | 25 users have rated this item.
 

std::array: The Secure, Convenient Option for Fixed-Sized Sequences

Migrate your fixed-sized sequences to std::array, which offers a secure, efficient, and convenient alternative to built-in arrays—sans the overhead of vector. 


advertisement
hen implementing a sequence of elements that needs to grow and shrink dynamically, std::vector is a fine choice. It provides the convenience of a standard container class, including automatic memory management, random access to elements, a uniform interface, and compatibility with standard algorithms.

However, for a sequence whose size is fixed, the performance hit that comes with all that convenience isn't always justifiable. In particular, a vector incurs the following overhead:

  • Nontrivial initialization and destruction. The constructor and destructor of std::vector are relatively costly. This overhead is particularly noticeable when your program creates a large number of vectors.
  • Allocation time. Vectors allocate storage for their elements at runtime.
  • Space overhead. A vector object typically occupies 32 bytes.

Built-in arrays carry none of this overhead. They allocate their storage statically and have zero space overhead. So why not just use built-in arrays instead for your fixed-sized sequences? Built-in arrays have their own downside: Their security loopholes make them an insecure choice.

Luckily, the C++ Standard Library offers a container-like template called std::array, which offers a secure, efficient, and convenient alternative to built-in arrays—without the overhead of vectors. This 10-Minute Solution shows how to use this powerful alternative for fixed-sized sequences.


How can I implement fixed-sized sequences without the security loopholes of built-in arrays or the noticeable overhead of vectors?


Use std:array as a secure, efficient, and convenient alternative to built-in arrays and vectors for your fixed-sized sequences.

  Next Page: Instantiation and Usage
Page 1: The std::array Class TemplatePage 3: Interfaces to the Standard Library
Page 2: Instantiation and Usage 
Please rate this item (5=best)
 1  2  3  4  5
advertisement