Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

More Thoughts About Arrays and Vectors

May 18, 2011

My recent comments about teaching vectors instead of arrays to C++ beginners has left an important idea unstated that I think it is worth mentioning explicitly.

One of the most fundamental differences between C and C++ is that C++ has constructors and destructors. This difference gives C++ a philosophical distinction that C lacks between an object and the memory that the object occupies. In C, when you write

int n;

you are defining n as a variable of type int with an undefined value. C++ behaves the same way, because int is a built-in type. However, in C++, when you write

std::string s;

you are not only defining s as a variable of type string, but you are also executing code — the string constructor — that causes the object denoted by s to come into existence.

Now consider an array definition:

string ss[30];

If the type of ss were int, we would just be allocating memory. However, because ss is a string, we are asking the compiler to construct 30 objects for us. Because C++ (potentially) executes code whenever an object is constructed or destroyed, our definition has turned into a request for action that we do not really need. The distinction between allocation and construction turns an innocent coding technique in C into a bad habit in C++.

To avoid this bad habit, it is necessary to think about containers as data structures with a number of elements that the programmer can change easily and efficiently. Built-in arrays do not allow the number of elements to be changed at all; "dynamic arrays" allocated with new and delete (or with malloc and free) allow the number of elements to be changed only at significant expense. Compared with either of these strategies, using vector, list, or deque executes faster and is also more convenient. In other words, the library containers are usually more efficient than built-in arrays, both in their use of computer time and their use of programmer time.

If you've been reading my articles for long, you know that I consider human time to be worth more than computer time. However, "less important" does not mean "unimportant"; we should always be alert to opportunities to make programming more efficient for both humans and machines.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.