how much does free free?

W

wojtek.fedorko

Hi,
Is there a way of knowing how much memory a call to free() actually
freed?
thanks,
Wojtek
 
I

Ian Collins

Hi,
Is there a way of knowing how much memory a call to free() actually
freed?

Yes, keep track of how big the object you allocated and freed was.
There isn't any other way.
 
P

pete m

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

from malloc.h:

struct mstats {
size_t bytes_total;
size_t chunks_used;
size_t bytes_used;
size_t chunks_free;
size_t bytes_free;
};

extern struct mstats mstats(void);
 
W

Wade Ward

pete m said:
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.)

from malloc.h:

struct mstats {
size_t bytes_total;
size_t chunks_used;
size_t bytes_used;
size_t chunks_free;
size_t bytes_free;
};

extern struct mstats mstats(void);
How widespread is this undocumented function? To be honest, it sounds like
a practical joke.
 
J

Jack Klein

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.
from malloc.h:

C does not have a header names malloc.h, either.

If you want to talk about non-standard, platform specific extensions,
post on groups for the platform.

--
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
 
J

Jack Klein

Hi,
Is there a way of knowing how much memory a call to free() actually
freed?
thanks,
Wojtek

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.

--
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
 
P

pete m

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

Jack Klein

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
 
R

Richard Tobin

If you want to talk about non-standard, platform specific extensions,
post on groups for the platform.

Apparently several platforms have the same extension. Do you suggest
newsgroups for all combinations of platforms?

-- Richard
 
R

Richard Tobin

pete m said:
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

Why is that? On a system with no alignment constraints, malloc(1) can
allocate exactly one byte if it wishes, and will naturally free() the
same amount.
and there is also overhead from the data structure used by the free list.

malloc() doesn't have to use a free list, though I believe most
implementations do. It could, for example, use a bitmap.

-- Richard
 
M

Mark McIntyre

Apparently several platforms have the same extension. Do you suggest
newsgroups for all combinations of platforms?

Two possibilities:

1) the different platforms have different extensions with the same
name. In this case, its /vital/ to post in the right platform
specific group.

2) the different platforms have an identical extension. In this case
it doesn't matter which platform-specific group one posts to.

Either way, one assumes that the OP (and you) can 'engage your brain'
before posting?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
F

Flash Gordon

Mark McIntyre wrote, On 30/04/07 23:29:
Two possibilities:

1) the different platforms have different extensions with the same
name. In this case, its /vital/ to post in the right platform
specific group.

2) the different platforms have an identical extension. In this case
it doesn't matter which platform-specific group one posts to.

You forgot the third possibility, there is a group of platforms
conforming to some additional standard, in which case post to a group
that deal with all platforms conforming to that standard. I'm thinking
of comp.unix.programmer here as opposed to, for example, an AIX specific
group.
Either way, one assumes that the OP (and you) can 'engage your brain'
before posting?

Agreed.
 
G

Giorgos Keramidas

How widespread is this undocumented function? To be honest, it sounds
like a practical joke.

Does it really matter? There is no "malloc.h" in the standard, so this
is not a question that is valid in the context of c.l.c.

For example, it can't be very widespread, since two of the Unix(R)
systems I often use for development work do not have mstats() or a
'struct mstats' definition in "malloc.h". Even more importantly, there
are dozens of malloc/free implementations out there, and I highly doubt
all of them use the simple model of "chunks" and "bytes" hinted at
above.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top