Connecting Tech Pros Worldwide Forums | Help | Site Map

how free() ,delete() works?

Ramki
Guest
 
Posts: n/a
#1: Mar 16 '06
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


Dietmar Kuehl
Guest
 
Posts: n/a
#2: Mar 16 '06

re: how free() ,delete() works?


Ramki wrote:[color=blue]
> 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()[/color]

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.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jacek Dziedzic
Guest
 
Posts: n/a
#3: Mar 16 '06

re: how free() ,delete() works?


Ramki wrote:[color=blue]
> 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)[/color]

It remembers it, "somewhere".
[color=blue]
> 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()[/color]

Might be, might not be.
[color=blue]
> Do anybody know more about this??[/color]

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.
Jack Klein
Guest
 
Posts: n/a
#4: Mar 17 '06

re: how free() ,delete() works?


On 16 Mar 2006 01:33:57 -0800, "Ramki" <writerama@gmail.com> wrote in
comp.lang.c++:
[color=blue]
> How does free() , delete() works?
>
> If we allocate memory using the following statement
>
> int *p = malloc(100*sizeof(int));[/color]

....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.
[color=blue]
> 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)[/color]

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Closed Thread


Similar C / C++ bytes