how free() ,delete() works?

R

Ramki

How does free() , delete() works?

If we allocate memory using the following statement

int *p = malloc(100*sizeof(int));

Inorder to release the memory we will call '"free(p) "

How does it know about how many bytes/bits to be deleted?(same is the
case with new and delete statements)

As far as i know, the information about this will be stored in the
previous byte of the begining of block which is allocated using
malloc()

Do anybody know more about this??

Thanx,
Rama
 
D

Dietmar Kuehl

Ramki said:
As far as i know, the information about this will be stored in the
previous byte of the begining of block which is allocated using
malloc()

There are many approaches how to determine the size of allocated
blocks. The functions may indeed use a word preceeding the actual
address. It is also possible to look up the address in some kind
of internal data structure which stores the common size of objects
in a pool. 'operator new()' can actually also take advantage of
the size passed to it from the compiler: the compiler knows the
size of the actual object being released, at least for non-array
object.
 
J

Jacek Dziedzic

Ramki said:
How does free() , delete() works?

If we allocate memory using the following statement

int *p = malloc(100*sizeof(int));

Inorder to release the memory we will call '"free(p) "

How does it know about how many bytes/bits to be deleted?(same is the
case with new and delete statements)

It remembers it, "somewhere".
As far as i know, the information about this will be stored in the
previous byte of the begining of block which is allocated using
malloc()

Might be, might not be.
Do anybody know more about this??

In short -- you are guaranteed about the compiler doing
it right, ie. remembering "somehow", but you do not have
any guarantees about where this information is stored.

Perhaps you can store a copy of this information yourself
upon allocating the memory? Having only the pointer, you
can't (in a portable way) determine the size of the block it
points to.

HTH,
- J.
 
J

Jack Klein

How does free() , delete() works?

If we allocate memory using the following statement

int *p = malloc(100*sizeof(int));

....then we'll get a diagnostic from the compiler, since the code is
invalid. malloc() returns a pointer to void, and you cannot assign a
pointer to void to a pointer to another object type without a cast in
C++, although you can in C.

Also it is much better to use the type of the pointer rather than hard
coding the type of the object. If you are going to use malloc() in
C++, you should use it like this:

int *p = (int *)malloc(100 * sizeof *p);

The reason for the second change is to prevent errors that can occur
if you have to change the data type. Suppose for example you realize
that you need floating point values, and change p to be a pointer to
double. In your version you would need the change 'int' to 'double'
in two places, and create potential horrible problems if you changed
one but missed the other. In my version, you merely change "int *p"
to "double *p", and the sizeof value is automatically correct.
Inorder to release the memory we will call '"free(p) "

How does it know about how many bytes/bits to be deleted?(same is the
case with new and delete statements)

It may store it physically in the aura of the programmer, or write it
in the pattern of clouds in the sky. Such a detail is completely up
to the compiler and its library. The C++ language does not specify
the mechanism.
 

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

delete[] 7
delete[] 9
delete[] p or delete[] *p 187
How to use parameter in operator delete? 2
Operator delete question 12
Different behavior overloaded global and local operator delete 0
free() 76
free memory? 15

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top