This C / C++ tutorial covers dynamic memory allocation, memory allocation (on heap), exception handling and some examples.
Examples are included of "C" malloc() and free() as well as "C++" new and delete.
C malloc() and free() do not call constructors or destructors.
C++ new and delete operators are "class aware" and will call class constructors when a class is allocated with new and a destructor when delete is called. [example]
Mixing malloc with delete or new with free is undefined.
In addition, C++ new and delete
operators were introduced with the C++ practice of allocating memory in
constructors and deallocating the same memory with a destructor.
new/delete advantages:
new/delete invokes constructor/destructor. Malloc/free will not.
new does not need typcasting. Malloc requires typcasting the returned pointer.
new/delete operators can be overloaded, malloc/free can not.
new does not require you to explicitly calculate the quantity of memory required. (Unlike malloc)
C dynamic memory allocation:
Use "malloc", "calloc" and "free": File: MallocTest.cpp
Note: The STL string class copy constructor is employed to return a
copy (return by value). Yes the variable "ost" is out of scope once we
leave the function, but the copy of its contents is valid.
Do not "return ost.str().c_str()" as this pointer is out of scope once
the procesing leaves the function and the data lost.
C++ dynamic memory allocation exception handling:
Use exception handling:
#include <iostream>
using namespace std;
main() { int ii; double *ptr[5000000];
try { for( ii=0; ii < 5000000; ii++) { ptr[ii] = new double[5000000]; } }
Run with fewer privileges: nice -n 19 AllocNewTest
C++ Virtual Destructors
Using polymorphism to delete dynamically allocated objects by
their base class pointer: Declare base class destructor virtual so that
the delete operator can be applied to the base class pointer. The
derived class destructor is called first, before the base class
destructor.
#include <iostream> using namespace std;
class Base { public: Base(){}; virtual ~Base(){ cout << "Base class destructor called" << endl; } };
class Derived : public Base { public: Derived(){}; ~Derived(){ cout << "Derived class destructor called" << endl; } };
main() { Base *ptr = new Derived(); delete ptr; }
Resutls:
Derived class destructor called
Base class destructor called
Note:
If the delete operator is applied to the base class and the
destructor is NOT virtual, then this will cause a memory leak as only a
portion of the memory is freed.
Base class destructor is not pure virtual (set =0) or there would be no base class implementation of the destructor..
Class contructors can NOT be virtual.
Books:
C++ How to Program
by Harvey M. Deitel, Paul J. Deitel
ISBN #0131857576, Prentice Hall
Fifth edition.
The first edition of this book (and Professor Sheely at UTA) taught me to
program C++. It is complete and covers all the nuances of the C++ language.
It also has good code examples. Good for both learning and reference.
Exceptional C++: 47 Engineering Puzzles, Programming Problems and Solutions
by Herb Sutter
ISBN #0201615622, Addison-Wesley Professional
Advanced C++ features and STL.
More Exceptional C++
by Herb Sutter
ISBN #020170434X, Addison-Wesley Professional
Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition)
by Scott Meyers
ISBN #0201924889, Addison-Wesley Professional
More Effective C++: 35 New Ways to improve your Programs and Designs
by Scott Meyers
ISBN #020163371X, Addison-Wesley Professional