Section 3 - Dynamic Memory Allocation
10A.3.1 Declaring and Initializing Primitive Types
The previous section was a little artificial because it implied that we tend to pair up a float pointer, fp with a float variable, x, and use the pointer to point to the variable:
float x, *fp; fp = &x;
And, while this is certainly possible, it's sort of a waste of time. I mean, why bother? We have a perfectly good float, x, so why do we need some esoteric pointer to refer to it? Why not just use the float variable x whenever we need it and lose the pointer. Good question, and that's why it isn't normally done that way.
In practice, we usually declare pointers without declaring any corresponding variables for them to point at.
float *fp;
Okaaaaay ... so what do we do with a pointer that doesn't point to anything? Stick with me here. We can create (out of thin air so-to-speak) a float to which fp will point. We use the new operator with the following syntax:
fp = new float;
This creates a float variable without a name of its own, one that we will "get to" through the float pointer fp. To access this variable (with no name of its own) we use the same syntax we learned earlier, that is we dereference fp with an asterisk: *fp. We use the construct *fp throughout the program, method or class (wherever fp is defined) to reference the nameless float that it manages:
*fp = 5.5 / 1.0003; *fp = *fp - 8.6; cout << *fp << endl;
I know, it still seems easier to use the float variable x from the start, but this is a huge step. It will have profound consequences in what comes. Not only that, but it lays the foundation for how all other modern programming languages declare and instantiate objects of classes. So if you learn it in C++ you will get the concept down automatically for Java, C#, Visual Basic, J#, etc.
This process is generally called dynamic memory allocation. It enables us to wait until we need a variable before we allocate it.
Let's look at the process again.
long *idNum; // declare a long pointer idNum = new long; // instantiate (allocate) a long (dynamically) *idNum = 1234567; // use the long by dereferencing the pointer cout << *idNum; // need * to get to the nameless long variable
10A.3.2 Freeing up Memory
As if this wasn't enough of a headache to do something that you probably think is more easily done without pointers. I've got more bad news for you. We're not done.
Unlike ordinary variables, when you allocate memory dynamically for a pointer, you have to deallocate manually before you are done using it. This is the opposite step of instantiation. It destroys the variable location that the pointer controls, and leaves the pointer undefined. You do this with the delete operator:
delete fp; delete idNum;
Once you delete memory like this, two things happen:
- The pointer is no longer valid. You cannot refer to *fp or *idNum until you re-allocate memory with another new operation.
- The memory that was allocated for the variable that the pointer pointed to is no longer taking up space. It can be used by the program or operating system.

10A.3.3 Using Dynamic Allocation in Programs.
As long as you can stomach the notation, using pointers is incredibly easy. Here is a nice example to help you confirm your understanding of the concept:
#include <iostream> #include <string> using namespace std; int main() { // declare pointers double *farenheit, *centigrade; string *name; // allocate (instantiate) variables dynamically farenheit = new double; centigrade = new double; name = new string; // get name and temperature from user cout << "What's your name? "; getline(cin, *name); cout << "\nFine, " + *name + ", now what is the temperature outside where" " you \nare right now (in farenheit)? "; cin >> *farenheit; // do the computation *centigrade = (5./9.)*(*farenheit-32.); // display one place after decimal point cout.setf(ios::fixed); cout.precision(1); // show results cout << "\nWell, if you were in Canada, it would be " << *centigrade << "C.\n\n"; // free memory delete farenheit; delete centigrade; delete name; return 0; }
The effect of this program should be transparent, but here is the screen shot so you can confirm your suspicions:
