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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With