What is a good metaphor for c memory management?

I am trying to find a good metaphor to explain the allocation of memory, initialization and release in c non-technical audience. I heard that pass-by-reference / value spoke well about the mail service, but not so much for distribution / release.
Therefore, since I was thinking about using the idea of ​​renting space, it might work, but I wonder if the SO team can provide something better.

+4
source share
8 answers

There are several metaphors you can use. The problem with something like renting a property is that the tenant usually chooses a space. In this case, the responsibility of the operating system is to select an adjacent physical space of sufficient size. It is more like a hotel. You request a room of the size you want, and the hotel staff will assign you a specific room that is not occupied and at least as large as you requested ( malloc ). They give you two things: the address (your room number) and permission to access this room exclusively (key). Then you decide when you want to check and return the key ( free up the room). After that, the hotel can assign a room to someone else.

+14
source

As an option about rental analogies, what about the analogies of a car park?

There are numbered spaces in the parking lot - these are your main units of memory (C chars - which sounds almost like cars ...;). If you want to park your car, it really doesn't matter what space you get - they are all pretty equal.

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

+3
source

What about containers? You get a bowl from the kitchen ( malloc ) and continue to put things into it. When you're done, you will either throw it in the trash (loss of pointer) or return it to the kitchen so that it can be reused ( free ). In the end, the kitchen ends from the bowl if you do not return them.

+2
source

Here is a metaphor:

At the restaurant (call it Mr. C Kitchen) to accommodate guests. They give out numbers (pointers) that guests (a bit of memory) can use to find their tables (memory blocks).

+1
source

I like the idea of ​​renting premises. I didn’t hear that before. Customers can rent small or large rooms. The keys to the leased space are similar to pointers.

Free memory is similar to the client, which returns the key and abandons its place, so the space can be recycled.

Space can be rented for an hour or forever. If the client rents the space forever, but loses the key and does not use this space, it is good that the memory leak is due to the fact that the space is not used and does not return back to the community.

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

+1
source

If you go with the analogy of a hotel room in Temik, then you can compare the violation of segmentation with an accidental entry (or even sleep) into another person's room. Garbage collection can be characterized by janitors :)

+1
source

Please do not use metaphors when you are teaching technical materials.

The only metaphor that I thought was useful was that a variable is a field in which you can store values. Everyone else was useless at best and was confusing in general.

Introduce the technical concept of dynamic memory management. This is not difficult, and if they really need a metaphor to understand what is happening, they certainly have other problems.

They need to understand memory management anyway when it comes to pointers (or references) in which most people leave the class because they don't get it.

EDIT: I forgot when you use the rental metaphor, you lose students when it comes to garbage collection or link counting methods. Obviously, this will confuse people later.

+1
source

I would use the land lease method. In any land management system, there is a designated body (private or public) to monitor land administration and management issues. Any body interested in obtaining a land plot requests to receive a plot of land (via malloc ). Then, the administrative body checks the availability of space and allocates space (provides land) and sends a map or address of this part of the land (rent for an almost unlimited period of time). After the client has served his interest on this plot of land (i.e. no longer interested in its storage), he then returns it back to the agency / administrator (using free ).

0
source

Source: https://habr.com/ru/post/1310794/


All Articles