The Locker Metaphor to Teach Dynamic Memory
Ricardo Jimhnez-Perk, Marta Patiiio-Martinez, J. Angel Vehzquez-lturbide
Universidad Polithcnica de Madrid
Depto. de Lenguajes y Sistemas InformAticos, Facultad de Informitica
Campus de Montegancedo s/n, 28660 Boadilla del Monte, Madrid, Spain
{rjimenez,mpatino,avelazquez}@fi.upm.es
Crist6bal Pareja-Flores
Universidad Complutense de Madrid
Depto. de InformGtica y AutomBtica, Escuela Universitaria de Estadistica
Avda. Puetta de Hierro s/n, 28040 Madrid, Spain
[email protected]
Abstract
The use of analogies in teaching abstract concepts is often
Some students experience difficulties when first introduced a key element in its pedagogy, as advocated in [2]. For
to dynamic memory. The goal of this paper is to present an instance, a well-known metaphor for introducing the
analogy behveen dynamic memory programming and a critical section problem in teaching concurrent
real-world example that will help students in understanding programming is that of Ben&i about igloos and eskimos
the underlying concepts behind dynamic memory: a left- PI-
luggage room with lockers.
In OUTwork, we begin by introducing dynamic memory
concepts intuitively by means of a metaphor. Once these
1 Introduction concepts are well understood, they are taught in a general
setting, as found in programming languages. We have
Dynamic memory (i.e. pointer variables and their use) is a chosen a concrete and well-known scenario, a left-luggage
crucial topic in imperative languages and is covered in room, to ease the comprehension of the abstract concepts.
many courses, typically in CS2. It also constitutes one of
the hardest topics in programming courses. The main The structure of the paper is as follows. First, we present
problem seems to be that dynamic memory concepts are our metaphor and its analogy with dynamic memory
too abstract. Paradoxically, these low level concepts are concepts. In section 3, we include several common
embodied in languages such as Pascal, by means of programming pitfalls, showing how the metaphor helps in
nonintuitive, abstract mechanisms. revealing them. The two last sections include a discussion
and our conclusions.
There are many ways of introducing programming topics
that enhance comprehension, e.g. program animation. We
use here a discourse that is commonly used in computer 2 The Locker Metaphor
science to deal with abstract and arbitrarily defined
concepts: metaphors. There are many examples of In this section, we present the elements of the metaphor.
successtil metaphors in this field: the desktop, the memory Each subsection presents new concepts and explains their
of computers, running programs, etc. Notice that the word correspondence in Pascal.
“metaphor” here is loosely used to refer to any image that
represents one thing and that is used instead of something
else to explain it better [3]. 2.1 Left-luggage room and locker keys
permission to make digitalhard copy of part or all of this work for personal
or classroom us8 is granted without fee provided that copies are not made A left-luggage room is a store of lockers, where each one
or distributed for proflt or commercial advantage, the copyright notice, the
title of the publication and its date appear, and notice is given that can be opened by means of its corresponding key, as
cop ing Is by psrmlsslon of ACM, Inc. To copy otherwise, tqrepublish, to shown in the figure below:
posYon servers, or to redistribute to lists, requires pnor specific permission
and/or a fee.
SIGCSE ‘97 CA, USA
0 1997 ACM o-89791.aa9-4~97looo2...$3.50
169
can be observed in the metaphor: when the contents of a
keyring is copied into another, the key in the destination
keyring is lost, with no chance of recovering it later.
2.3 Asking for a locker
When someone needs a locker, he or she goes to the locker
manager office, gives the manager a keyring, and asks him
for a locker. The manager will take any available key and
put it in the keyring (the previous key, if any, is thrown
away). Although the way the manager bookkeeps keys
could be considered irrelevant to the user, it can help to
fully understand certain kinds of errors. We can imagine
that at the manager offrce there is a board with numbered
hooks (as can be seen in the figure below), one per locker.
If a key is hanging on a hook, then its locker is free,
otherwise it is assigned.
Each key has a number which corresponds to the number
of the locker it opens. The dynamic memory area is very In a programming language, the dynamic memory manager
similar to a left-luggage room, where memory cells are is in charge of cell bookkeeping. When a cell is needed, the
lockers and memory addresses are locker keys; as each New primitive is invoked, returning the address of a free
locker can be opened with its key, a cell can be accessed by cell. We can imagine some kind of bookkeeping similar to
its associated memory address. Notice that neither keys that of the manager, but its details are irrelevant.
nor memory addresses can be modified (both are literal
values).
2.2 Keyrings
In our metaphor, keyrings are used to hold keys. Each
keyring can hold at most one key (i.e. zero or one).
Keyrings and keys play the role of pointer variables and
address values, respectively, in imperative programming
languages. Keyrings represent pointer variables that can
hold a locker key (a memory address). A keyring holding
no key represents the NIL value of a pointer variable. Two
keyrings can hold a copy of the same key; in this case, they
can open the same locker. This situation corresponds to the
fact that two pointer variables can point to the same
memory cell, that is, they can contain the same memory
address.
Simple pointer operations can also be explained graphically
with the locker metaphor. Comparison of pointers
2.4 Liberating a locker
(keyrings) is straightforward: it will be successful only if
both keyrings are empty or the keys contained in both
When a locker is no longer needed, the user notifies the
keyrings open the same locker.
locker manager, and gives him the keyring with the key of
the locker. The manager makes a copy of the key, hangs it
Assignment of pointer variables corresponds to the
up on its hook, indicating that the locker is available again,
following procedure: if the source keyring is empty, the
and returns to the user the keyring with the old key. Notice
target keyring is left empty; otherwise, a copy of the key in
that surprisingly, the user keeps his copy of the key, This
the source keyring is made, the old key in the destination
part of the metaphor corresponds to this situation: when a
keyring (if any) is thrown away, and fmally the copy is
memory cell is not needed anymore, it must be freed by the
introduced into the target keyring. An important property
170
--_ .-
Dispose operation, but this operation could leave the neither a new locker was given, nor were the contents of
pointer with its previous value. the locker duplicated.
Let us consider this fact in our metaphor by means of the
2.5 Equivalent concepts following sequence of operations.
To summarize, a table with the equivalence between the Wehave twokeyringskl and k2. We ask the locker
metaphor and dynamic memory concepts is shown. managerfor a key and he puts it in keyringkl . The
manager assigns us a locker (say, number 25).
memory pool left-luggage room Now, we can open the locker with the key in
memory manager locker manager kqring kl and put $1,000 in. Ajterwards, we
pointer keyring duplicatethe key in keyringkl andput the copy in
locker keyring k2. Then we use the key in keyring k2 to
memory cell
open the locker and take out the money. When we
address key of a particular locker
open again the locker withthe key in kl, there is
list of available cells board hooks with a key
no money.
kl, k2: ^Type kl and k2 are keyrings
New (kl) ask for a locker, get its key In the example it is clear that copying keyrings (pointers) is
and put it in keyring kl not a way to make more money, because the keys are
kl* open the locker whose key is copied, but not the locker contents.
in kl to access its contents
kl = NXL check whether kl is empty This example is equivalent to the following Pascal code
kl = k2 check whether kl and k2 are segment:
both empty or both contain
keys that open the same VAR
locker kl, k2 : ^INTEGER;
kl:= NIL leave empty keyring kl
kl:= k2 if k2 is empty then leave kl New(k1) ;
kl^:= 1000;
k2:= kl;
k2^:= 0;
Write (kl*)
that will display number o on the screen.
3 Common Pitfalls 3.2 Memory leakage
There are many examples of situations (i.e. uses of pointer A common programming mistake consists in forgetting to
variables) that the metaphor helps to illustrate graphically. free unused cells. In the metaphor, it is evident that a locker
We include in this section several common pitfalls. First, can only be reused if its key is returned to the manager. On
we give a description of each one in terms of the metaphor. the other hand, if the locker key is lost, neither the locker
An advantage is that the underlying concepts of dynamic will be used again nor will it be assigned to someone else,
memory can be illustrated independently from the since no one has its key.
programming language being used. For the sake of
completeness, we include the corresponding Pascal code. This mistake can be illustrated by the following example:
We have the kqring kl. We askfor a locker to
3.1 Copying pointers and cells the locker manager, whose key will be put in
kqvring kl. (Although it is irrelatant, we can
A frequent error occurs when students, intending to suppose that we are assigned locker 47.) We
duplicate the contents of a cell, resort to assigning pointers. open the locker with the key in keyring kl and
The source of this error is that they think that copying put $1,000 in. Finally, we throw the key in the
pointers implies the duplication of the pointed cell. river, leaving the keyring empty. Whenwe try to
Following our metaphor, the copying of a key simply open the locker, wefind out thatwe do not have
results in having two keys of the same locker. Obviously, itskey.
171
New (k2) ; {it happens to point to
The assigned locker (locker 47) will never be reused, as the the same cell as kl}
locker manager should have received its key back. Even k2A := 1000;
worse, we will not be able to open the locker in order to get kl^ := 0 ; {the money is stolen}
our money. Write (k2”)
The corresponding Pascal code is: Notice that this program is useful for illustrating a bad use
of pointers, but reading the program is more didactic than
VAR running it. In fact, only when both variables are assigned
kl : ^INTEGER; the same address will it print the expected value of 0.
New (kl);
kl* := 1000;
3.4 Uninitialized pointer variables
kl := NIL;
Write (kl^)
Another frequent error consists in using pointer variables
The program will yield a run-time error. that have not been initialized, as if they were pointing to a
dynamic variable. In the metaphor this situation can be
modelled as follows. At the beginning, keyrings can hold
3.3 Misuse of liberated cells old keys (i.e. keyrings with previously used keys). For this
reason, the keys they contain should not be used, since
Another common mistake is trying to use cells that were they open lockers we are not authorized to operate, To use
previously liberated. In the metaphor, the situation is as a keyring for the first time, we have to empty it or to ask
follows. Once a locker has been fi-eed, it can be assigned to for a new locker with it. Thus, the old key is not used,
someone else. However, the previous owner still keeps a
copy of the key, so two different people can access the It is straightforward to think of a more concrete scenario
same locker, only one being authorized. As a consequence, and to show its correspondence in Pascal,
the correct working of the locker system depends on the
goodwill of its users. This situation can be illustrated by the
following example: 4 Discussion
We have @ring kl and Peter has keyring k2. In the previous sections, we have intermingled the
We ask the locker manager for a key to be metaphor and dynamic memory concepts. We have
placed in keying kl. (Suppose we are assigned proceeded in this way in order to explain it to the reader,
locker number 18.) Now, we can use the locker. but the metaphor is usually used in a different way during
Later, weffee the locker (number I8), showing lectures. We prefer to start first by introducing the left-
the key in kl to the locker manager. Recall that b3gw metaphor, and by highlighting the
we stillkeep the key. misunderstandings and errors described in section 3. Then,
we introduce proper dynamic memory programming,
Later, Peter asksfor a locker with keyring k2. relating its concepts to the metaphor when necessary, thus
Suppose thathe is assigned the same locker (in providing students with a concrete basis to refer to
thiscase, locker 18). Peter thenputs $1,000 into whenever a concept is too abstract to comprehend,
the locker. Ifwe open the locker now (remember
we still keep the key of locker 18), we can take Our metaphor can be adapted to cope with several data
the moneyporn it. WhenPeter opens the locker types, by providing several kinds of lockers. However, as
again, he willfind out thatthere is no money. the metaphor gets more complicated, we prefer to restrict
our examples to use a simple data type (e.g. INTEGER).
In Pascal:
The metaphor can also be extended to deal with linked
VAR lists, by considering that the contents of the locker consists
kl, k2 : ?NTEGER; of two shelves, one to store an object (e-g, money), and the
other for storing a keyring. The keyring in the second shelf
New (kl) ; can be used to hold a key of yet another locker; thus,
... useof kl* ... lockers can be linked. This extension of the metaphor is
Dispose (kl) ; useful to introduce linear data structures in a concrete way,
. .. However, we find it more adequate to use the metaphor in
172
situations similar to the ones expressed in this paper. References
Students can easily understand the corresponding Pascal
code and can always refer to the metaphor when in doubt. 1. Ben&i, M. Princ@les of ConcurrentProgramming.
Prentice-Hall, 1982.
Metaphors used for learning in computer science fall into 2. Cole, J. D. While loops and the analogy of the single
three overlapping streams [4]. Operational approaches stroke engine. SIGCSE Bulletin 23, 3 (Sept. 1991),
focus on their measurable effect on learning. Structural 20-22.
approaches develop formal representation of relations 3. Johnson, G. J. Of metaphor and the difficulty of
between the source and the target domain. Pragmatic computer discourse. Communicationsof the ACM 37,
approaches acknowledge that metaphors are incomplete, 12 (Dec. 1994), 97-102.
but claim that their power may be attributed to such 4. Madsen, K. H. A guide to metaphorical design.
disparities, We designed our metaphor following the Communicationsof the ACM37, 12 (Dec. 1994), 57-
second approach because we wanted a bijection between 62.
the elements of the metaphor and those of dynamic
memory.
Our metaphor has a number of important features, some of
them because it is structural. First, it is simple since it
refers to a familiar situation. Second, it has a rich structure,
which enables a clear illustration of all the concepts and
activities involved in handling dynamic memory. We can
easily show correct and incorrect situations as illustrated by
the pitfalls described above. Third, its expressivity enables
us to explain dynamic memory in terms of this unique
metaphor. This is an important advantage because mixiig
several metaphors can confirse beginners [3].
5 Conclusions
A pedagogic metaphor for introducing dynamic memory
programming has been presented. The metaphor helps
students to understand concepts of dynamic memory in a
concrete way. It also allows them to work with dynamic
memory before it is presented as a programming language
mechanism, in a more abstract way. As a consequence, we
can highlight errors in a natural way, thus paving the way
for a better understanding of sometimes hard to grasp
abstract concepts.
We have no formal statistics measuring the impact of the
metaphor on our students. Nevertheless, our experience has
shown that using the metaphor to illustrate abstract
concepts has lead to an overall better understanding of the
abstract concepts underlying dynamic memory
management.
Acknowlegments
We want to thank Sami Khuri for his comments and
suggestions which greatly helped to improve the paper.
173