Next Previous Contents

4. C++ Zap (Delete) command

The delete and new commands in C++ are much better than the malloc and free functions of "C". Consider using new and zap (delete command) instead of malloc and free as much as possible.

To make delete command even more cleaner, make a Zap() command. Define a zap() command like this:


#define zap(x) if (x) { delete(x); x=0; }

Then, assuming all pointers start their lives as null pointers, I can safely delete what's been newed, and not delete things that haven't been newed, with a series of zap() commands like this:


        zap(pchFirstname);
        zap(pchLastname);
        zap(pchJobDescription);

There is nothing magical about this, it just saves repetative code and makes it more readable. Do not stick a typecast in the zap() command -- if something errors out on the above zap() command it likely has another error somewhere.

Also my_malloc() , my_realloc() and my_free() should be used instead of malloc(), realloc() and free(), as they are much cleaner and have additional checks. For an example, see the file "mychar.h" which is using the my_malloc() and my_free() functions.


Next Previous Contents