Working of the delete [ ] operator

M

Megha Vishwanath

Hi,

I'd like to know how heap aggregates created with a "new" operator at
non-contiguous memory locations get deallocated using the "delete []"
operator in VC++.

A garbage collectors in Java deallocate on the basis of a record
maintained during the "new" allocation.

Do we use something like a garbage collector to maintain arecord of
the heap memory start indexes.

Please enlighten me.

Thanks,

Megha.
 
T

tom_usenet

Hi,

I'd like to know how heap aggregates created with a "new" operator at

You mean the new[] operator?
non-contiguous memory locations get deallocated using the "delete []"
operator in VC++.

There is a header before the bit of memory that new[] returns that
gives the size of the allocated block. A call to delete[] then uses
this size to
a) work out how many objects to destruct
b) actually deallocate the memory
A garbage collectors in Java deallocate on the basis of a record
maintained during the "new" allocation.

Same in C++ - the memory allocator keeps track of the size of
allocations. Different allocators do this differently.
Do we use something like a garbage collector to maintain arecord of
the heap memory start indexes.

No. You should read up on memory allocators:

http://www.memorymanagement.org

Tom
 
R

Rolf Magnus

tom_usenet said:
Hi,

I'd like to know how heap aggregates created with a "new" operator at

You mean the new[] operator?
non-contiguous memory locations get deallocated using the "delete []"
operator in VC++.

There is a header before the bit of memory that new[] returns that
gives the size of the allocated block. A call to delete[] then uses
this size to
a) work out how many objects to destruct
b) actually deallocate the memory

Just to mention: That's one way to do it. The C++ standard doesn't
specify how this is to be done, and there are other ways.
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top