Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
152 C++ Coding Standards 77. Use vector and string instead of arrays. Summary Why juggle Ming vases? Avoid implementing array abstractions with C-style arrays, pointer arithmetic, and memory management primitives. Using vector or string not only makes your life easier, but also helps you write safer and more scalable software. Discussion Buffer overruns and security flaws are, hands down, a front-running scourge of to- day's software. Silly limitations due to fixed-length arrays are a major annoyance even when within the limits of correctness. Most of these are caused by using bare C-level facilities--such as built-in arrays, pointers and pointer arithmetic, and man- ual memory management--as a substitute for higher-level concepts such as buffers, vectors, or strings. Here are some reasons to prefer the standard facilities over C-style arrays: They manage their own memory automatically: No more need for fixed buffers of "longer than any reasonable length" ("time bombs" is a more accurate descrip-