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.
views:
240answers:
8How 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.
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.
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).
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.
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 :)
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
).
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.
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.