CPP Using the Free Store

Managing data on the free store for effective processing

  • You can think of the free store as a massive section of memory, which is free for you to use.
  • The advantage to the free store is that the memory you reserve remains available until you explicitly state you are done with it by freeing it.
  • Forget to free up the used memory, can cause the system to crash.
  • The advantage of accessing memory in this way, rather than using global variables, is that only functions with access to the pointer (which has the appropriate address) have access to the data.
  • For this to work, you must be able to create a pointer to an area on the free store and to pass that pointer among functions.
  • We use the new keyword to create space on the free store. This will also return a pointer (address) to that space.
  • To free up that space on the free store we use the keyword delete.
    Example of creating and accessing the data on the free store
    unsigned short int * pPointer=0;
    // Allocate memory on the free store for a short int
    pPointer = new unsigned short int;
    // Set the value of the free store location to 72.
    *pPointer = 72;
    Example of deleting the data on the free store
    // De-allocate memory on the free store for a short int
    delete pPointer;

Rules handling the free store

There are some good programming rules to follow when you work with pointers and the free store:
  1. Initialize always the pointer variable with zero (0).
  2. After you have deleted a pointer, to free up memory, set always the pointer variable to zero (0).
  3. Test always the pointer variable for zero (0) to ensure that it is free to use.
The summary of this should then be:
// When Initializing the pointer
unsigned  int * pNumber =0;
...
// When later using the pointer
if (pNumber!=0) {
  delete pNummer;
  pNummer=0;
}
pNumber= new unsigned  int;
...

What about constructors and destructor for new created objects.

Each time an object is created then a constructor will be performed, and each time an object is deleted, the destructor will be performed.

You should be aware of when a constructors and when the destructor is performed for objects that are created and deleted on the free store.

Here is an example:
#include <iostream>
using namespace std;
class Person {
  public:
     Person();
     ~Person();
  private:
     int Age;
};
Person::Person(){
   cout << "Constructor called. " << endl;
   Age = 1;
}
Person::~Person(){
   cout << "Destructor called. " << endl;
}
int main() {
   cout << "Person Finn... " << endl;
   Person Finn;
   cout << "Person *pPers = new Person..." << endl;
   Person * pPers = new Person;
   cout << "delete pPers... " << endl;
   delete pPers;
   pPers=0;
   cout << "Exiting, watch Finn go... " << endl;
   return 0;
}
When we run this application, the result will be:
Person Finn...
Constructor called.
Person *pPers = new Person...
Constructor called.
delete pPers...
Destructor called.
Exiting, watch Finn go...
Destructor called.

You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.