how much memory to free?

S

siddhu

Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be freed
and make it available for further allocations?

Regards,
Siddharth
 
H

Harald van =?UTF-8?B?RMSzaw==?=

siddhu said:
Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be freed
and make it available for further allocations?

However much you asked for will be freed. The necessary information for this
is recorded when you call malloc.
 
M

Malcolm McLean

siddhu said:
Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be
freed and make it available for further allocations?
The secret is that a control block exists _before_ the pointer returned by
malloc(). free() simply subtracts a few bytes to access it.

Not all mallocs work exactly like this, but a vanilla memory allocation
system does.
 
F

Flash Gordon

Malcolm McLean wrote, On 09/09/07 18:02:
The secret is that a control block exists _before_ the pointer returned
by malloc().

Or after, or somewhere else entirely.
free() simply subtracts a few bytes to access it.

Only on some systems.
Not all mallocs work exactly like this, but a vanilla memory allocation
system does.

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".

To the OP, you don't need to know how it knows, all you need to know is
it gets handled for you.
 
K

Keith Thompson

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.)
 
F

Flash Gordon

Richard Tobin wrote, On 10/09/07 00:40:

That would be a neat trick. How does it work?[/QUOTE]

A fat pointer with half of it pointing at the data and half pointing a
the control block ;-) Or maybe just the length.

Alternatively, and slightly more seriously, there could be a region used
to store all the control blocks which is after all of the malloced regions.

However, it is all done by magic so as the programmer you don't need to
know. :)

OK, so I posted without my brain engaged. However some real
implementations don't store a control block before malloced region.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top