Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good metaphor for c memory management? [closed]

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.

like image 398
Michael Chinen Avatar asked Nov 28 '22 19:11

Michael Chinen


2 Answers

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.

like image 31
caf Avatar answered Dec 15 '22 04:12

caf


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.

like image 59
Eric Mickelsen Avatar answered Dec 15 '22 04:12

Eric Mickelsen