In this lab, we’ll show how you can redefine the new/delete operators to achieve a variety of useful or bizarre results.
The files for this lab are available in ~cs253/Labs/New.
Consider glob.cc. It redefines the global
delete operators to add trace output. This could be useful in
figuring out just when
delete are being invoked.
Note the function signatures—at this level,
new takes a
and returns a
void *, whereas
delete takes a
void * and
Consider class.cc. It redefines the
delete operators to add trace output.
This will not affect
delete operations on other types,
main, which function is called and why?
deletefor a specific class
Try to compile forbid1.cc. Does it compile? Why not?
Try to compile forbid2.cc. Does it compile? Why not?
Compile and execute errchk.cc. Observe the error message, and decide what caused it. How would you improve it to detect multiple deletions of the same memory? (You don’t need to write the code, just explain it.)
If you’re concerned about the contents of deleted memory getting reused, then consider the technique used in scrub.cc. What criteria would you use to decide on a value for memory? It should be:
For example, a repeated string of “Jack” or “WTF!” would fit all of the above requirements. Notably, an odd value makes for a poor pointer, and would often cause a segmentation violation upon use.
Now, run pool.cc.
Of course, it’s a pain to have to redefine
for every class. After we study templates, you will be able to
create a templated mixin to do the same for all classes.
class Foo, and then derive
Barinherit the redefined
sizeof(Bar) > sizeof(Foo)?