views:

240

answers:

8

I'm trying to find a good metaphor to explain memory allocation, initialization and freeing in c to a non technical audience. I've heard pass-by-reference/value talked about quite well with postal service usage, but not so much for allocation/deallocation.
So for I've thought about using the idea of renting a space might work, but I wonder if the SO crew can provide something better.

+2  A: 

How about containers? You get a bowl from the kitchen (the malloc), and proceed to put stuff in it. When you're done, you either throw it in the trash (losing the pointer), or bring it back to the kitchen so it can be reused (free). Eventually the kitchen runs out of bowls if you don't bring them back.

Yann Ramin
+1 fitting metaphor - and you might mention that malloc doesn't do the washing up, so the bowl you get is often dirty, containing other people's leftovers. a little squirt of memset cleans them up nice.
mdma
+13  A: 

There are a number of metaphors you might use. The problem with something like property rental is that the renter generally chooses the space. In this case, it's the operating system's responsibility to choose a contiguous physical space of sufficient size. It's more like a hotel. You request the size room you want and the hotel staff will assign you a particular room that is unoccupied and is at least as large as you requested (malloc). They give you two things: the address (your room number) and permission to access that room exclusively (the key). It is then up to you to decide when you would like to check out and give back the key (free up the room). After that, the hotel can assign the room to someone else.

Eric Mickelsen
In addition, if you walk away without returning the key (`free` ing the space) then the hotel won't be able to use that room for anyone else. If enough clients do that the hotel will have to shut down- or, in terms of your program, it will leak memory until it is no longer able to run.
fbrereto
Dropping the pointer without freeing is like losing your key instead of giving it back to the staff. And referencing memory you've already freed is like trying to get back into your room after you've checked out. ...@fbrereto: good call - you beat me to it.
Eric Mickelsen
it's good, until the hotel clerk looks at you strangely for requesting a room for 16 people...
mdma
@mdma: That's entirely possible. The operating system is allowed to throw an exception if there is no contiguous memory block large enough.
Eric Mickelsen
Thanks, this is nice and closer to what I was looking for than my initial idea. Although I must admit I laughed out loud at mdma's comment. I wonder if we can get closer.
fsmc
Its important to note that this hotel does not have housekeeping either. Your rooms will likely be in a mess (not zeroed) when checking in.
Yann Ramin
In other words, it's a cheap motel that can neither provide housekeeping nor any other service except giving you a room. Sounds pretty normal to me. :P
Dustin
You could probably also illustrate how the underlying OS (in this case, the management) moves people around (paging in/out, obeying a request to lock memory, etc).
Tim Post
This is a great analogy.
Mike Weller
+1  A: 

Here's a metaphor:

A restaurant (call it Mr. C's Kitchen), has to accommodate guests. They hand out numbers (pointers) that guests (bits of memory) can use to find their tables (memory blocks).

Chetan
+1  A: 

I like the idea of space rental. Hadn't heard that one before. Clients can rent small or large spaces. Keys to the rented space are like pointers.

Freeing memory is analagous to a client returning the key and giving up his space, so the space can be recycled.

Space can be rented for an hour or forever. If a client rents the space forever but loses the key and doesn't use the space, well, that's a memory leak because the space isn't used and is not recycled back to the community.

An important point is that if a client returns his key, but keeps a copy he sews the seeds of a problem. The space will be recycled to a new official owner, but the old owner can unofficially return and trash the space after gaining access with his unauthorized key.

Bill Forster
I like what you say about what the old owner could trash the place if he saves the key (pointer). Hadn't thought of that.
fsmc
+1  A: 

If you go with tehmick's hotel room analogy, then you could liken a segmentation violation with accidentally entering (or even sleeping in) someone else's room. Garbage collection can be characterised by cleaning maids :)

Johan
+1 - Good call.
Eric Mickelsen
A: 

I would U use the land lease process. In any land administration system there is a designated body(private or public) to oversee the land administration and management issues. Any body who is interested in getting some land space, requests for a piece of land(through malloc). The admin body then check for a free space and allocate the space (grant the land) and send the map or address of the that piece of land (the lease is for virtually unlimited period of time). After the customer served his/her interest on that piece of land(i.e. has no more interest on keeping it), it then return it back to the agency/admin body (using free).

yona
+3  A: 

As a variation on the rental analogy, what about a car park analogy?

The car park has numbered spaces - those are your basic units of memory (C chars - which sounds almost like cars... ;). If you want to park your car, it doesn't really matter which space you get - they're all pretty equal.

You can even explain memory fragmentation this way - if you want to park your B-double semi-trailer, it's not just enough to have 6 spaces - you'll need 6 adjacent parking spaces.

caf
That's good. There's also a car park attendant who records which spaces are available. If you drive off without telling the attendant your space is empty, he doesn't know somebody else can use it. Eventually, the car park has no cars in it but also no free spaces. Conversely, if you tell the attendant your space is free, but you leave your car in it, there is always a chance that somebody else will park their car in it, pushing your car off the cliff (the car park is next to a cliff).
JeremyP
+1  A: 

Please don't use metaphors when you teach technical stuff.

The only metaphor i thought was useful was the one that a variable is a box where you can store values. All others were useless at best and confusing in general.

Just present the technical concept of dynamic memory management. It's not difficult and if they really need a metaphor to understand what is going on they have are definitely other problems.

They need to understand memory management anyhow when it comes to pointers (or references), which is the point where most people leave the class because they don't get it.

EDIT: I forgot, when you use the rental metaphor you students are lost when it comes to garbage collection or reference counting techniques. Its obvious that this will confuse people later.

Lothar
There are useful analogies, especially when explaining a concept to a non-technical audience. The car-based deadlock vs. the multithreaded mutex one. The idea of a queue. Some of these programming constructs come out of our understanding of natural phenomenon, in which case an analogy is appropriate. This comment seems to imply that the computer world is an entirely abstract concept that comes out of the void, when in fact it is somewhat abstracted concepts that have connections to our understanding of the non-computer world.
fsmc
I agree with Lothar. And I actally taught 20 students to use C pointers without any analogy whatsoever. "A pointer is a number" -- and that number represents a *location* in your computer memory. What really helps is to give several examples -- from silly ones to more involved (but not too much). *All* students learnt it after one single class.
Jay