|
|
|
|
Exercises
Solutions to selected exercises
can be found in the electronic document The Thinking in C++ Annotated
Solution Guide, available for a small fee from
www.BruceEckel.com.
- Create a class Counted
that contains an int id and a static int count. The
default constructor should
begin:
Counted( ) : id(count++) {. It
should also print its id and that it’s being created. The
destructor should print that it’s being destroyed and its id. Test
your class. - Prove to
yourself that new and delete always call the constructors and
destructors by creating an object of class Counted (from Exercise 1) with
new and destroying it with delete. Also create and destroy an
array of these objects on the
heap.
- Create a
PStash object and fill it with new objects from Exercise 1.
Observe what happens when this PStash object goes out of scope and its
destructor is
called.
- Create a
vector< Counted*> and fill it with pointers to new Counted
objects (from Exercise 1). Move through the vector and print the
Counted objects, then move through again and delete each
one.
- Repeat Exercise
4, but add a member function f( ) to Counted that prints a
message. Move through the vector and call f( ) for each
object.
- Repeat
Exercise 5 using a
PStash.
- Repeat
Exercise 5 using Stack4.h from Chapter
9.
- Dynamically
create an array of objects of class Counted (from Exercise 1). Call
delete for the resulting pointer, without the square brackets.
Explain the
results.
- Create an
object of class Counted (from Exercise 1) using new, cast the
resulting pointer to a void*, and delete that. Explain the
results.
- Execute
NewHandler.cpp on your machine to see the resulting count. Calculate the
amount of free store available for your
program.
- Create a
class with an overloaded operator new and delete, both the
single-object versions and the array versions. Demonstrate that both versions
work.
- Devise a test
for Framis.cpp to show yourself approximately how much faster the custom
new and delete run than the global new and
delete.
- Modify
NoMemory.cpp so that it contains an array of int and so that it
actually allocates memory instead of throwing bad_alloc. In
main( ), set up a while loop like the one in
NewHandler.cpp to run out of memory and see what happens if your
operator new does not test to see if the memory is successfully
allocated. Then add the check to your operator new and throw
bad_alloc.
- Create
a class with a placement new with a second argument of type
string. The class should contain a static vector<string>
where the second new argument is stored. The placement new
should allocate storage as normal. In main( ), make calls to
your placement new with string arguments that describe the calls
(you may want to use the preprocessor’s __FILE__ and
__LINE__
macros).
- Modify
ArrayOperatorNew.cpp by adding a static vector<Widget*> that
adds each Widget address that is allocated in operator new(
) and removes it when it is released via operator delete(
). (You may need to look up information about vector in your
Standard C++ Library documentation or in the 2nd volume of this book,
available at the Web site.) Create a second class called MemoryChecker
that has a destructor that prints out the number of Widget pointers
in your vector. Create a program with a single global instance of
MemoryChecker and in main( ), dynamically allocate and
destroy several objects and arrays of Widget. Show that
MemoryChecker reveals memory
leaks.
[50]
There is a special syntax called placement new that allows you to call a
constructor for a pre-allocated piece of memory. This is introduced later in the
chapter.
|
|
|