how free() works

R

riva

We merely pass the pointer to dynamically allocated variable to free()
function.
How does it decide about the number of bytes to be freed?
 
Z

Zara

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
 
V

Vinay

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

Chris Dollin

riva said:
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.
 
M

Malcolm McLean

Vinay said:
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?
 
R

Richard

Chris Dollin said:
`malloc` and `free` are friends and have no secrets from one
another.

Different implementations, like different friends, have different
secrets.

Nice explanation and analogy.
 
C

CBFalconer

riva said:
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.
 
C

CBFalconer

Flash said:
CBFalconer wrote, On 26/12/07 21:11:

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 */
 
F

Flash Gordon

CBFalconer wrote, On 27/12/07 21:14:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top