Flash Gordon said:
Malcolm McLean wrote, On 09/09/07 18:02:
Or after, or somewhere else entirely.
Only on some systems.
What you mean is that is how it works on some systems, whilst others
work differently. I don't see food flavourings have to do with it. At
least, I can't find references to a memory allocation system called
"vanilla".
"Vanilla" is obviously a colloquial term for "ordinary".
To the OP, you don't need to know how it knows, all you need to know
is it gets handled for you.
Agreed.
It can be useful, though, to see an example of *how* the size can be
stored. If you're not familiar with memory allocation, being told
that free "just knows" how many bytes to release can be confusing.
That's why the question "How does free know how many bytes to free?"
is a FAQ (7.26, to be precise). Understanding that, *for example*,
the size can be stored just before the allocated chunk, perhaps along
with some other metadata, tells you that there's nothing magical about
it.
But it's also very important to know that that's just one possible
strategy. For example, an implementation that stores all its metadata
(i.e., everything other than the actual allocated chunks of memory) in
a separate location can be more robust in the presence of code that
writes past the end of an allocated object.
(I think K&R has a sample memory allocator; reading that code can be
instructive.)