473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how free() works

We merely pass the pointer to dynamically allocated variable to free()
function.
How does it decide about the number of bytes to be freed?
Dec 26 '07 #1
10 1548
On Tue, 25 Dec 2007 22:16:22 -0800 (PST), riva <ra*********@gmail.com>
wrote:
>We merely pass the pointer to dynamically allocated variable to free()
function.
How does it decide about the number of bytes to be freed?

It is implementation dependent. Usually, the chunk of bytes realy
assigned with malloc is at least sizeof(size_t) bytes larger then the
request. These extra bytes are used to store the size of the chunk,
and they are 'before' the pointer returned. But please remember thet
each compiler is free to do as it likes, so thi is no rule of thumb.

Best regards,

Zara
Dec 26 '07 #2
Guys..i want to do the following!

I want to edit a text file through my C program.

The text file is as follows
1
2
3
4

i want to change the contents to

1
2
100
3
4

Thanks in advance..
Dec 26 '07 #3
riva wrote:
We merely pass the pointer to dynamically allocated variable to free()
function.
How does it decide about the number of bytes to be freed?
`malloc` and `free` are friends and have no secrets from one
another.

Different implementations, like different friends, have different
secrets.

--
Conspiracy Hedgehog
Meaning precedes definition.

Dec 26 '07 #4
riva <ra*********@gmail.comwrites:
We merely pass the pointer to dynamically allocated variable to free()
function.
How does it decide about the number of bytes to be freed?
The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've just asked
question 7.26.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 26 '07 #5
"Vinay" <vi*****@gmail.comwrote in message
Guys..i want to do the following!

I want to edit a text file through my C program.

The text file is as follows
1
2
3
4

i want to change the contents to

1
2
100
3
4

Thanks in advance..
Please start a new thread for a new topic.

The problem hasn't been very well specified.
You can achieve the output demanded simply by calling fopen() fprintf() and
fclose(), with the name of the file you wish to overwrite.
However this is unlikely to be what you really want. You'll have to ask for
a slightly better formal spec. Do you want to insert an arbitrary
user-specified number in the second line, of arbitrary input text, for
example?

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Dec 26 '07 #6
Chris Dollin <eh@electrichedgehog.netwrites:
riva wrote:
>We merely pass the pointer to dynamically allocated variable to free()
function.
How does it decide about the number of bytes to be freed?

`malloc` and `free` are friends and have no secrets from one
another.

Different implementations, like different friends, have different
secrets.
Nice explanation and analogy.
Dec 26 '07 #7
riva wrote:
>
We merely pass the pointer to dynamically allocated variable to
free() function. How does it decide about the number of bytes to
be freed?
Raw black magic.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Dec 26 '07 #8
CBFalconer wrote, On 26/12/07 21:11:
riva wrote:
>We merely pass the pointer to dynamically allocated variable to
free() function. How does it decide about the number of bytes to
be freed?

Raw black magic.
Nope. On mine the black magic has been grilled under a low heat.
--
Flash Gordon
Dec 27 '07 #9
Flash Gordon wrote:
CBFalconer wrote, On 26/12/07 21:11:
>riva wrote:
>>We merely pass the pointer to dynamically allocated variable to
free() function. How does it decide about the number of bytes to
be freed?

Raw black magic.

Nope. On mine the black magic has been grilled under a low heat.
Don't you find that that allows the basic greasy black material to
leak out, so that heavily realloced buffers tend to fail to free
abandoned buffers, resulting in memory leaks?

Code such as the following can be used to detect this sort of
'black leak':

size_t ct
char *p, *t;

for (p = NULL, ct = 0; ++ct;) {
if (t = realloc(p, ct)) p = t;
else break;
}
if (t == p) /* no realloc failure occured */
analyzemem(p); /* system dependant code */
}
else failanalyze(p); /* also system dependant */

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Dec 27 '07 #10
CBFalconer wrote, On 27/12/07 21:14:
Flash Gordon wrote:
>CBFalconer wrote, On 26/12/07 21:11:
>>riva wrote:

We merely pass the pointer to dynamically allocated variable to
free() function. How does it decide about the number of bytes to
be freed?
Raw black magic.
Nope. On mine the black magic has been grilled under a low heat.

Don't you find that that allows the basic greasy black material to
leak out, so that heavily realloced buffers tend to fail to free
abandoned buffers, resulting in memory leaks?
No, since the black magic is grilled *before* it is used for
malloc/realloc/free.
Code such as the following can be used to detect this sort of
'black leak':

size_t ct
char *p, *t;

for (p = NULL, ct = 0; ++ct;) {
if (t = realloc(p, ct)) p = t;
else break;
}
My optimiser replaces this entire loop with:
p = __malloc_largest_possible_block(&ct);
t = 0;
so no reallocing actually occurs.
if (t == p) /* no realloc failure occured */
analyzemem(p); /* system dependant code */
}
else failanalyze(p); /* also system dependant */
--
Flash Gordon
Dec 27 '07 #11

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

Similar topics

74
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
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
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
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
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
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...
11
by: vjay | last post by:
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?
7
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.