CS 4448 - Spring 1998
Object-Oriented Programming and Design
Talk 7.2.1
by
Kavita Ravi
Memory Allocation in C++ and C compatibility
by
Andrew Koenig
Why aren't "free" and "new" compatible? Why should [] be specified for deleting
for an array if the size isn't necessary?
-
If new were implemented with a different call than malloc
(this flexibility should be provided), then compatibility with free
will not always be possible.
Destructors:
-
Destructors must be executed over all objects being destroyed to free
up dynamic memory. If the array were not indicated by [], then the
number of objects should be indicated to delete. One solution is
to write the size of the object at the beginning of the memory
allocated for it. But since delete uses free sometimes
(for built-in types), this compatibility will be destroyed by specifying
the size.
One Solution:
-
Objects that are vectors (arrays) are stored in a table as an (address,
size) pair. Scalars are not stored in the table. delete will
be interpreted as a scalar being freed. delete [] will be treated as
a vector and the table is looked up to find out the number of objects
in this array. (The destructor can then be executed over all the objects
in this array.
Garbage Collection in C++ - No panacea but useful
by
Andrew Koenig
Why isn't Garbage Collection a standard in C++?
Garbage Collection
-
The user doesnt have to take care of destroying created objects.
-
Garbage collection is normally done using "use-counts".
-
Assuming garbage collection is done, when should destructors be
executed? During garbage collection? Memory overhead in storing
objects.
-
If a destructor allocates some memory, infinite allocation-free
looping.
-
Problems with initialized pointers. Objects may have spurious pointers to them.
-
Garbage Collection has memory overhead.
-
Conservative collection - one solution. Hard to quantify the performance
standard.
Garbage collection will be useful in applications where objects are more uniform
in nature. Until there is a good scheme to specify performance, it will
not become a standard.
Copyright © University of Colorado. All rights reserved.
Revised: March 4, 1998