Chapter 5 Pointers and Dynamic Memory
Address Operator & Dereference Operator
Arrays are
useful
for data storage, when you know an exact size for the memory allocation
for the array.
However, if the size is not set, an array may waste
space or it may run out of space. Therefore, we use
to allocate and deallocate memory spaces as we need it when we need it.new
delete
The new operator: obtain
memory from the free storage.
It returns an address of variable stored.
Return 0(NULL) if there is no enough space.
Note: new is followed by the type of
the address.
The new operator automatically creates an object of proper size,
and remains a pointer of the connect type.
--------------------------------------------------------------------------------
On page 229 of Data Structures with C++ using STL, second edition, by
William Ford & William Topp:
You can think of the heap as a bank of memory, much like a financial--------------------------------------------------------------------------------
bank that maintains a reserve of money. A program can borrow
(allocate) memory from the heap when additional storage space is required
and then pay it back (deallocate) when it is no longer needed.
Click here for an example.
Allocating Memory:
int * ip;
ip = new
int;
// Tell the computer to allocate memory for an integer variable
*ip =
7;
// You can't get to the contents of the new variable except
through the pointer ip
int *ip = new int(7);The above three statements can also be written as
int
numOfElements;
cout << "How
many numbers do you want to store?" << endl;
cin >>
numOfElements; // Let the user choose the
number of elements he needs
int * ip;
ip = new
int[numOfELements]; // Allocate just enough space
for the array that the user needs
Releasing Memory:
To free the
space for this object, use the delete operator.
int *value = new
int(7);
delete value;
int *months =
new int[12];
delete [] months;
//delete the array (note the [])
Example of insert elements in an ascending order list.
Example:
Dynamic Class:
| Constructor | Destructor |
| (1) Name is same as class | (1) Name is same as class but ~ in front. |
| (2) It does not have return type. | (2) It does not have return type. |
| (3) It can have parameters, also can specify default for parameter | (3) It does not have any parameter. |
| (4) There is only one constructor for a class, but can be overloaded. | (4) There is only one destructor per class. |
| (5) It initializes objects | (5) It is to release memory allocated to objects on a heap. |
|
|
the object itself | ¡@ |
|
|
the data value of member1 | (*this).member1 |
|
|
the pointer of member2 | (*this).member2 |
Note: The -> operator is a shortcut of dot
operator with a pointer
The
* operator has a lower precedence than the dot operator..
For
example,
#include <iostream.h>
class square
{
private:
float side;
public:
square(float s)
{
side = s;
}
float area(void)
{
return side * side;
}
};
void main()
{
square Pool(4.0), *PrtPool;
PrtPool = &Pool;
cout<< "The area is " << (*PrtPool).area()
<< endl;
cout<< "The area is " << PrtPool->area()
<< endl;
}
miniVector Class: click here to see program 5-3
Matrix Class: click here to see program 5-4