Re: how much does free free?
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Sat, 28 Apr 2007 00:24:12 -0500
On 27 Apr 2007 22:14:44 -0700, pete m <pmac360@xxxxxxxxx> wrote in
comp.lang.c:
On Apr 27, 8:58 pm, Jack Klein <jackkl...@xxxxxxxxxxx> wrote:
On 27 Apr 2007 20:06:23 -0700, pete m <pmac...@xxxxxxxxx> wrote in
comp.lang.c:
wojtek.fedo...@xxxxxxxxx wrote:
Hi,
Is there a way of knowing how much memory a call to free() actually
freed?
thanks,
Not in general. However, if your system supports the undocumented
function mstats(), you can call it before and after free and take the
difference. (You may have to use debug malloc, which is horrible.)
Your reply, in addition to being off-topic, will be totally useless to
the OP if his (unnamed) platform is not the same as your (unnamed)
platform.
This group discusses the standard C language. It does NOT have any
undocumented functions.
In fact, all of the answers posted so far are wrong in one way or
another.
My answer was off topic; yours gives the wrong value.
Free does not free the same amount of memory as the size of the malloc
request. At the very least, free must release a multiple of 4 or 8
(or possibly 16) bytes, and there is also overhead from the data
structure used by the free list. And presumably one reason someone
might ask this question is to find out what the system overhead
actually is.
Obviously you did not read my reply very carefully. To make it easy
for you, here it is again:
"Yes, it freed exactly as many bytes, no more and no less, as were
allocated by the call to an allocation function (malloc(), calloc(),
or realloc()) that supplied the pointer value. Unless the pointer
value is NULL, in which case free() frees exactly 0 bytes, that value
was at least as many bytes as you asked for when you called that
allocation function."
You will notice that I did not say that free releases the same amount
of memory as the size of the malloc request. I said it releases as
much memory as it allocated, which is *** at least *** as much as was
requested if the allocation was successful.
And no, there is neither guarantee nor requirement that free() must
release a multiple of some number of bytes, it releases exactly as
much as was allocated. Some implementations might only allocate in
blocks that are multiples of some number of bytes, which is an
entirely different story.
As for a "data structure" and "the free list", neither of these things
is defined by the standard, or even required.
One of the reasons that such things are not defined by the C standard
is that there are many ways for the implementation to provide them,
some of them quite different from those you might happen to be
familiar with.
As for speculation about the OP wanting to know about the overhead, if
any, of memory allocation on his system, there is certainly nothing in
the wording of his post to suggest that. But if that is what he's
looking for, he's still asking in the wrong place. He would need to
ask in a group that supports his particular compiler/OS combination,
which might be completely different from you particular compiler/OS
combination.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- how much does free free?
- From: wojtek . fedorko
- Re: how much does free free?
- From: pete m
- Re: how much does free free?
- From: Jack Klein
- Re: how much does free free?
- From: pete m
- how much does free free?
- Prev by Date: Re: syntax error with pointer to pointer
- Next by Date: Re: Style vs function? in arrays
- Previous by thread: Re: how much does free free?
- Next by thread: Re: how much does free free?
- Index(es):
Relevant Pages
|