Overview
MEMORY ALLOCATION
The stack and the heap are memory areas used by the program.
The stack is used to store parameters passed to functions, the return address for the function call and local variables.
Note that static and local variables are not stored on the stack. Declaring a variable places it on the stack whereas allocating space for it dynamically stores in on the heap.
DYNAMIC MEMORY ALLOCATION (new AND delete)
new is an improved version of the C malloc() function and delete is based on the C free() command. They are used in situations where you need to declare variables as the program is running - i.e. dynamically. They are normally controlled by the use of pointers.
#include <iostream.h>
void main(void)
{
int *NewPtr; // declare a pointer to point to integers
NewPtr = new int (24); // define a new variable and give it
// the value 24
cout << *NewPtr << endl;
delete NewPtr; // once the variable has been finished with,
// delete it.
}
new and delete can also be used to dynamically allocate memory for arrays:
#include <iostream.h>
void main(void)
{
int *NewPtr;
NewPtr = new int [20];
// commands
delete [] NewPtr; // notice array size is not stated
}
In a similar fashion, class objects can by dynamically allocated and de-allocated using new and delete.
#include <iostream.h>
class trivial
{
public:
int x;
trivial() { x = 0; }
~trivial() { cout << "\nDestructing...."; }
};
void main(void)
{
int count;
int MaxSize = 20;
trivial *TrivPtr;
TrivPtr = new trivial [MaxSize]; // declare a new type of
// array of type trivial and
// point to it.
for (count = 0; count < MaxSize; count++)
{
(TrivPtr + count)->x = count; // access c and then point to the
// next array element
cout << (TrivPtr + count) ->x << endl;
}
delete [] TrivPtr;
}
Class instances declared with new are not automatically deallocated when they go out of scope - i.e. the destructor is not called. To deallocate a dynamically allocated variable, the delete command
delete is not a command. While not actually incorrect, delete can be thought of more like C's free function
must be used.delete is not a command. While not actually incorrect, delete can be thought of more like C's free function
EXERCISE 20
Design a class with a constructor that says "new instance". Every time the n is pressed, a new instance of this class is to be made. Each new instance is to be stored in an array. When the d key is pressed, the instance most recently created is to be deleted.
THE this POINTER
The this pointer points to the value of the current instance of a class.
Normally this is used implicitly within a member function for data member references. Use the this special pointer to refer to the object currently being executed. That is, returning this from a class object's member function returns a pointer to the object.
Example use:
return this; // return pointer to current object
The this pointer points to the object in which it was used. If you want to return the object itself rather than a pointer to it, use *this. For further study I have included the explanation for this from the Microsoft Visual C++ help files:
this
The this pointer is a pointer accessible only within the member functions of a class, struct or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call
myDate.setMonth(3);
can be interpreted this way:
setMonth(&myDate,3);
The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class. For example:
void Date::setMonth(int mn)
{
month = mn; // these three
this->month = mn; // statements are
(*this).month = mn; // equivalent
}
The expression (*this) is commonly used to return the current object from a member function.
Note : Modifying the this pointer is illegal in the latest version of C++
THE NULL POINTER
NULL is a symbolic constant which is defined in several header files. It contains the value 0.
NULL may not be 0
NULL is commonly used with pointers to signify the success or failure of an operation.
For example : the fopen() function can be used to open a file and associate a stream with it. If the file is successfully opened, fopen() returns a pointer to the FILE data structure that controls the stream. In the case of an error fopen() returns NULL.
While none of this is incorrect, it would have been a far better idea to show how to use this in C++
Ignoring the usual namespace and void main errors, I can't understand why try, throw and catch aren't handled as they are incredibly important. In C, it would be remiss to test the pointer for the memory allocation to ensure that it's not NULL. C++ supplies the methods to ensure the allocation has been followed.