473,508 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Free() works?

Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?

Nov 14 '05 #1
11 2318

"vjay" <vi***********@gmail.com> wrote in message
news:6c******************************@localhost.ta lkaboutprogramming.com...
Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?


Magic.

Seriously, the internal details of how 'free()'
works is an implementation-dependent issue, often
depending upon the workings of the host platform.
IOW it can easily be completely different among
implementations. All you need to know is that
it 'just knows'.

http://www.eskimo.com/~scs/C-faq/top.html
See item 7.26

-Mike
Nov 14 '05 #2
vjay wrote:
Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?


From an interface perspective, the important thing to remember is that
you must pass free() a pointer returned by a previous
malloc/calloc/realloc call, and the amount passed to that original call
indicates how many bytes it frees. You can't, for example, allocate an
array and then free an element of it; free(a[5]) would attempt to free
the space the pointer in a[5] *points to*.

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #3
Derrick Coetzee wrote:
vjay wrote:
Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?


From an interface perspective, the important thing to remember is that
you must pass free() a pointer returned by a previous
malloc/calloc/realloc call...


True enough. From a comp.lang.c point of view the primary response is
that the internal implementation of free() is system-specific and not
on-topic here. The C standard specifies that free() must behave in
such-and-such a way, but says nothing about how that is to be
achieved. How to implement free() is an interesting programming topic
in its own right, but not specifically a C question.

(Note: but it is possible to implement the malloc()/free() couple
in standard C, and you can read about that in K&R2, the second edition
of Kernighan and Ritchie's "The C Programming Language").

Alin Cottrell
Nov 14 '05 #4
vjay wrote:

Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?


It depends.

It is reasonable to surmise that the memory allocation subsystem
keeps track of this information internally. As an exercise, write
a simple memory allocation subsystem yourself, with the interface:

void *my_malloc(size_t);
void my_free(void *);

Doing this will help you to understand.
Nov 14 '05 #5
Allin Cottrell wrote:
.... snip ...
(Note: but it is possible to implement the malloc()/free() couple
in standard C, and you can read about that in K&R2, the second
edition of Kernighan and Ritchie's "The C Programming Language").


No it isn't. It is necessary to make assumptions about at least
alignment. Thus malloc and friends must be system specific, and
supplied as part of the implementation.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #6
vjay wrote:
Can someone help me understanding how free call works.how the free call
will know that how many bytes has to free when i send pointer to that
call?


Probably not anyone in comp.lang.c, where we talk about standard,
platform-independent C. The details of how free() works live at a
different level, that of the specific implementation.

You'd do better looking up references to the C library on the
platform you're most interested in.

Allin Cottrell
Nov 14 '05 #7
vjay wrote:
Can someone help me understanding how [the] free call works?
How [does] free know how many bytes [must be] free'd
when I [pass] a pointer to free?


In the typical implementation,
the size of the allocated memory is stored in a free list.

I used Google

http://www.google.com/

to search for

+"free list" +"malloc" +"free"

and I found lots of stuff including:

http://www.cs.utk.edu/~plank/plank/c...2/lecture.html

Please tell us which compiler and operating system you are using
and we may be able to direct you to a forum where there are experts
on your particular implementation.
Nov 14 '05 #8
"Allin Cottrell" <co******@wfu.edu> wrote
vjay wrote:
Can someone help me understanding how free call works.how the free
call will know that how many bytes has to free when i send pointer to
that call?


Probably not anyone in comp.lang.c, where we talk about standard,
platform-independent C. The details of how free() works live at a
different level, that of the specific implementation.

Some insight into how to implement a standard library function is I think
topical, though of course we cannot speak for every compiler.

The trick is to store internal information in the block of memory
immediately before the pointer returned by malloc(). This will include the
size of the block allocated, and pointers to the next and previous blocks.
free() then uses this information to mark the block as freed, and if
necessary to merge it with adjacent free blocks ready for the next
allocation.
Nov 14 '05 #9
Thank you very much guys.The link by robert was useful.I shouldn't have
asked the question since the topic is so general but comp.lang.c is the
one i visit frequently.

Nov 14 '05 #10
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"Allin Cottrell" <co******@wfu.edu> wrote
vjay wrote:
Can someone help me understanding how free call works.how the free
call will know that how many bytes has to free when i send pointer to
that call?
Probably not anyone in comp.lang.c, where we talk about standard,
platform-independent C. The details of how free() works live at a
different level, that of the specific implementation.


Some insight into how to implement a standard library function is I think
topical, though of course we cannot speak for every compiler.


The problem is that there's rarely _the_ way to implement a Standard
function. There are usually several ways. For example...
The trick is to store internal information in the block of memory
immediately before the pointer returned by malloc().


....this is simply wrong. That is _one_ possible way to implement a
malloc() package, and a popular one; but it is by no means the only way,
or even the only good way, and to suggest that this is "the trick" is
misleading.

Richard
Nov 14 '05 #11
Richard Bos wrote:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"Allin Cottrell" <co******@wfu.edu> wrote
vjay wrote: Can someone help me understanding how free call works.how the
free call will know that how many bytes has to free when i send
pointer to that call?

Probably not anyone in comp.lang.c, where we talk about standard,
platform-independent C. The details of how free() works live at
a different level, that of the specific implementation.


Some insight into how to implement a standard library function is
I think topical, though of course we cannot speak for every
compiler.


The problem is that there's rarely _the_ way to implement a
Standard function. There are usually several ways. For example...


You can find one example, system specific to DJGPP, on my site
below, download section (nmalloc.zip). It will probably also
function on most Unices, but is definitely not portable according
to the standards we use here.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

74
7876
by: John Wells | last post by:
Yes, I know you've seen the above subject before, so please be gentle with the flamethrowers. I'm preparing to enter a discussion with management at my company regarding going forward as either...
9
2388
by: zerro | last post by:
Hello, I try to understand heap overflows (under Linux), but can not understand one thing with freeing memory allocated with malloc(). Here it comes: I have a program called 1.c: main() {...
11
426
by: weaselboy1976 | last post by:
Hello Does anyone know of a good website that actually describes and demonstrates WHY freeing a pointer more than once is a problem. I'm specifically interested in what the ill effects are. ...
74
3943
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
6
2464
by: Code Raptor | last post by:
Folks, I am hitting a segfault while free()ing allocated memory - to make it short, I have a linked list, which I try to free node-by-node. While free()ing the 28th node (of total 40), I hit a...
17
2121
by: kj | last post by:
How can one test if a pointer has been "freed" (i.e. with free())? My naive assumption was that such a pointer would equal NULL, but not so. Thanks, kj -- NOTE: In my address everything...
7
2135
by: slashdotcommacolon | last post by:
Hello, I'm working on the exercises from k&r, exercise 5-13 is to implement a simple replacement for the unix tail command. The brief says it should be able to cope no matter how unreasonable the...
3
2264
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
9
2555
by: dreiko466 | last post by:
(sorry about my english) Hello, i am a newbie in C and i am trying to understand how to use free(...) and malloc(...) functions. In the following programm i successfully create a point of...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.