Concepts / Container Overview

Container Overview

A container is an object that holds elements, and each element is one of the items it holds.

  • Programming

Why One Value Is Not Enough

Many programs need to hold more than one value at a time. A list of student scores, a set of file names, or a queue of tasks all share the same need: a single object that holds a group of related items. This article introduces that idea under the heading Container Overview and names the two things to understand first: the elements a container holds, and range checking, the way we make sure we only reach into the parts of a container that really exist.

Keep a single question in mind as you read. If you ask a container for a position, how can you tell whether anything is actually stored there? The rest of this article builds the mental model you need to answer that question reliably.

What do you think happens?

Suppose a container holds exactly four elements, and the positions that hold them are 0, 1, 2, and 3. Is a request for position 4 valid or invalid? Make your prediction before reading on.

  • Valid, because the container has a fourth element

  • Invalid, because no element is stored at position 4

Reveal answer
Answer:

Invalid, because no element is stored at position 4

With four elements at positions 0 through 3, position 4 lies one past the last stored element. The request points at nothing the container holds, so the range check should flag it.

What a Container Holds

Definition

A container is an object that holds elements, and each element is one of the items it holds. The container is the outer thing, and the elements are the items inside it.

The relationship between a container and its elements is one to many. One container can hold many elements, and each element belongs to that container. You can think of the container as a box at the top of a diagram, with each element hanging beneath it as one of the things it holds. The elements are what make the container useful: without them, the container would have nothing to offer.

Elements and range checking are linked ideas. Elements are what a container holds, and range checking is how you determine whether a particular location within the container refers to one of those elements. You need a set of elements before you can ask whether a position falls inside it, so the elements come first.

Position 072Position 185Position 290Position 364Position 4no element
Which positions in this container hold an element, and which positions fall outside the elements it stores?

In this illustration the container is a small list of four student scores. Positions 0 through 3 each hold one element, while position 4 has nothing stored in it. The numbering here is a convention chosen for the illustration; the point is that the container's elements define which positions are real.

Range Checking as a Question

Definition

Range checking is the act of confirming that a requested position falls within the range of elements a container actually holds.

A request for a position is only useful when that position exists. Asking a container for something beyond the elements it holds means asking for something that is not there. Range checking is the habit of verifying the request before relying on its result. Treat out-of-range requests as cases you must handle, rather than assuming the element exists.

Key point

A range check is a question about the container's current contents: is this position within the set of elements that are actually held right now?

Tracing a Range Check Step by Step

The reasoning a range check follows always has the same three steps: identify the range, compare the request against it, and decide. Work through the illustration from earlier, a container holding elements at positions 0 through 3, and trace two different requests.

Worked example

Checking a request for position 4

A container holds four elements at positions 0, 1, 2, and 3. A program requests position 4. Is the request valid?

  1. 1
    Identify the range

    The elements held occupy positions 0 through 3. The lowest valid position is 0 and the highest is 3.

  2. 2
    Compare the request

    The requested position is 4. Is 4 at least 0? Yes. Is 4 at most 3? No.

  3. 3
    Decide

    The request is above the highest held position, so it falls outside the range. The check fails and the request must be handled as an out-of-range case.

Result

Invalid. Position 4 holds no element, and the container is unchanged by the check.

Now change the request to position 2. The same three steps apply. The range is still 0 through 3. Is 2 at least 0? Yes. Is 2 at most 3? Yes. The comparison shows the position lies within the elements held, so the range check passes. The container was not changed by the check; it only confirmed that position 2 points at a stored element.

Requested positionfor example 4At least 0?lowest held positionAt most 3?highest held positionValid positionan element is held hereOut of rangehandle as a special casecompareyesnoyesno
What sequence of comparisons happens for a requested position, and at which step does it end as valid or invalid?

Read the flowchart from the top. For the request 4, the first comparison passes because 4 is not below 0, so the path moves to the second decision. There, 4 is not at most 3, so the path ends at the out-of-range outcome. For the request 2, both comparisons pass and the path ends at the valid outcome. In both cases the container itself is left exactly as it was.

Boundary Cases at the Last Element

Most range errors happen at the edges of the range. Compare two requests that sit right next to each other: one that is just inside the last element and one that is exactly one past it. The difference between them is small, but the outcome is completely different.

Position 3last held positionValidelement 64 is heldPosition 4one past the lastInvalidno element is heldwithin rangeoutside range
How does the result differ when a position is just inside the last element versus exactly one past it?

Both requests are one step apart, yet one passes and one fails. This is why the check must compare against the actual last held position rather than an approximate one. Stopping one short or reaching one past is exactly the kind of boundary mistake that a careful range check prevents.

Mistakes When Reasoning About Ranges

  • Common mistake

    Assuming a requested position exists because the container was created

    What it looks like

    A container of four elements is created, and the program reads position 4 without checking it.

    Why it fails

    Creating a container does not guarantee that every position you might ask for holds an element. The source treats out-of-range requests as cases to handle, not as cases that quietly succeed.

    Do this instead

    Run a range check on the requested position before relying on the result.

  • Common mistake

    Treating the position just past the last element as valid

    What it looks like

    With elements at positions 0 through 3, the learner accepts position 4 because it looks close to the end.

    Why it fails

    A position must fall within the elements actually held. Position 4 lies one past the last held element, so it is outside the range.

    Do this instead

    Compare the request against the highest held position and confirm it is at most that value.

  • Common mistake

    Believing the range check changes the container

    What it looks like

    A learner runs a range check and expects it to add an element when the request is out of range.

    Why it fails

    A range check only reads the container's current contents. It does not add, remove, or alter elements.

    Do this instead

    Treat the check as a question about the current contents, then handle the result separately.

  • Common mistake

    Confusing the container with one of its elements

    What it looks like

    A learner describes the whole list of scores as if it were a single score.

    Why it fails

    The container is the outer object, and each element is one of the items inside it. The relationship is one container to many elements.

    Do this instead

    Name the container as the holder and the elements as the individual items it contains.

Predicting Validity for New Requests

Use the same container from this article, holding four elements at positions 0 through 3. Before checking the answers, apply the three steps to each request below: identify the range, compare the request against it, and decide whether it is valid.

EASY

A program requests position 3 from the container. Is the request valid? Explain which comparison decides your answer.

Hints
  • Find the highest position that holds an element first.
  • Ask whether the request is at most that highest position.
MEDIUM

A program requests position 5 from the same container, and then requests position 0. Predict the result of each request, and state which one changes the container, if either does.

Hints
  • Position 5 is two past the last held position, so check the upper comparison.
  • A range check never changes the container, so neither request alters its elements.

Summary of the Container Model

  1. 1
    A container is an object that holds elements, and each element is one of the items it holds.
  2. 2
    Range checking confirms that a requested position falls within the elements the container actually holds.
  3. 3
    A range check reads the current contents and never changes them.
  4. 4
    Out-of-range requests are cases to handle, and the most common errors occur at the boundary just past the last held element.

At a glance

  • A container is an object whose job is to hold elements, and each element is one of the items inside it.
  • Range checking confirms that a requested position falls within the elements the container actually holds.
  • A range check follows three steps: identify the range, compare the request against it, and decide valid or invalid.
  • A range check only reads the container and never adds, removes, or alters its elements.
  • Boundary positions, especially the one just past the last element, are where range mistakes most often happen.

Related concepts

More concepts starting with C