473,545 Members | 2,043 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 2322

"vjay" <vi***********@ gmail.com> wrote in message
news:6c******** *************** *******@localho st.talkaboutpro gramming.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********@yah oo.com) (cb********@wor ldnet.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.e du> 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

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

Similar topics

74
7896
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 a MySql shop or a Postgresql shop. It's my opinion that we should be using PG, because of the full ACID support, and the license involved. A...
9
2393
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() { char *a, *b, *c, *d, *e; a = malloc(8);
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. Also, if you know of any really good books that describe everything about memory in a c program ... Thanks in advance!
74
3956
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 memory when we use malloc(). Plz help......
6
2468
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 segfault. This is legacy code. I tried debugging this problem, and am not able to come up with a valid reason for this. Following function is being...
17
2129
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 before the first period is backwards;
7
2141
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 input and should make best use of availbale storage (I suppose it means dont just declare a big char array). I have made a solution that seems to...
3
2269
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 these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the...
9
2557
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 struct(local_train) which carries the firstblock and last block address of a double linked list(-wagon-wagon-wagon-) which carries points of structs(train_load)...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.