Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

C++ Primer 5th Edition, Part 5: Core Language Versus Library?

August 15, 2012

One of the most important problems to consider in planning a C++ curriculum is how to teach the relationship between the core language and the standard library. In theory, the two are independent: It is possible to write C++ programs that do not use the standard library at all — particularly if an alternative input-output library is available. Of course, the core language is always available, and this constant availability implies that programmers who learn to use it can always count on that knowledge being useful.

Because every C++ programmer can count on the core language, it is tempting to bias C++ teaching in its favor. This bias shows up most strongly in how a C++ book treats two common data structures: strings and arrays. If you're willing to teach the library from the beginning, you can use examples such as:

 
     string hello = "Hello";
     string world = "world";
     string helloworld = hello + ", " + world;

If you insist on sticking to the core language, you have to use examples such as:

 
     const char* hello = "Hello";
     const char* world = "world";
char* helloworld = new char[strlen(hello) + strlen(world) + 2];
strcpy(helloworld, hello);
strcat(helloworld, ", ");
strcat(helloworld, world);

Aside from its verbosity, this second example has four problems. First, it doesn't really avoid the standard library, because it uses strcpy, strcat, and strlen. Of course we might argue that these three functions are so simple that programmers can implement those functions by themselves if they want to avoid using the standard ones. However, this strategy leads to the second problem: Both the implementation and the use of these functions requires a great deal of extra knowledge. Time that students spend acquiring that extra knowledge is time that they're not spending on learning how to solve the problems that really matter to them.

The third problem is that the second example is incomplete: It allocates memory for helloworld but never frees it. Therefore, an intellectually honest explanation of the second example should really include an extra statement

 
     delete [] helloworld;

at a suitable place near the end of the program, along with an explanation of why the brackets are necessary, even though some implementations will work just fine without them.

The fourth problem is that code like the second example is hard to get right. To prove it, I've made a typical beginner's mistake in this example. Sharp-eyed readers are invited to find it before I explain it next week.

Obviously, I think it's a good idea to rely on the standard library from the beginning of the curriculum. As a result, it will probably not surprise you to learn that C++ Primer, 5th Edition starts its detailed coverage of strings in Chapter 3, on page 81 of a nearly 900-page book. For contrast, I looked at the table of contents of another well-known C++ book, which I'd rather not name. What I will say is that it's 1,300 pages long and, as far as I can tell, does not discuss strings in detail until Chapter 16 (page 951).

Maybe I have some kind of a conceptual blind spot, but I don't understand what advantages come from deferring the standard library for that long. Even if the goal is to teach students how to program at a low level, I think that they are much less likely to become frustrated, and therefore likely to learn more in less time, if they start with high-level abstractions and then, once they have learned how to use them, take the covers off and look inside to see how they work.

I invite comments, especially from readers who think that it is more effective to teach low-level concepts first.

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.
 


Video