C++ Memory, Pointers and Classes

Overview and aims

  • This lecture will give an overview of memory management in C++ and how to create and use classes in C++
  • By the end of the lecture you will be able to:
    • Understand how memory is allocated
    • Use pointers and manage memory
    • Define and instantiate classes

Memory

  • One of the big difference between java/processing and c++ is how memory is handled
  • In java everything is done for you behind the scenes, in c++ you have to do it yourself

Java: what happens behind the scenes

  • Variables are put in a piece of memory called the stack
  • Each time a method is called its variables are added to the end of the stack
  • when it returns, they are removed from the stack

The Stack

	int a = 1;
	float b = 2.0;
	int c = 3;
	f(b,c);

The Stack

	int a = 1;
	float b = 2.0;
	int c = 3;
	f(b,c);
	void f(float x, int y)
	{
		int z = 3;
		// do stuff
	}

The Heap

  • This works fine for small types like ints and floats
  • Arrays and classes are too big for the stack
  • They are put in “The Heap”
  • A big area of general purpose memory
  • a reference to the class or array is put on the stack

The Stack

	int a = 1;
	float b = 2.0;
	int c = 3;
	f(b,c);
	void f(float x, int y)
	{
		int z [] = new int[20];
		// do stuff
	}

Pointers

  • The variable $z$ that is stored on the stack is not the array itself
  • It is the “address” of the place on the heap where the array is stored
  • This is called a pointer
  • A variable that “points” to a bit of memory

Memory in C++

  • It works in exactly the same way, except it is not behind the scenes
  • You can explicitly choose what goes on the stack and the heap

Pointers in C++

  • pointer variables have a * in front of them
	// an object of the boid class declared on the stack
	boid b1;                
	// a pointer to a boid object 
	// (doesn't point to anything yet)
	boid *b2;              
	// a boid object declared on the heap
	boid *b3 = new boid();  
	// b2 now points to the same thing as b3
	b2 = b3;

Pointers in C++

  • to get at the object pointed (dereference the pointer) you use a *
      // copies b2 into b1
      b1 = *b2;

Accessing Methods

  • to get at the methods of an object through a pointer you use ->
      // calls the draw method of b2
      b2->draw();
  • to get at the methods of a non-pointer object you use , like in java
      // calls the draw method of b1
      b1.draw();

Memory Management

  • When you’ve finished with a piece of memory on the heap you should release it
  • Otherwise you quickly run out of memory
  • Java does this automatically (garbage collection)
  • In C++ you have to do it yourself

Memory Management

  • every time you allocate some memory with \emph{new}
      boid *b2 = new boid();
  • you have to release it with \emph{delete}
      delete b2;

Destructors

  • The easy way to do this is with destructors
  • Like Java, every class has a constructor, which is called when it is created
  • It has the same name as the class
      boid()
      {
      	// set up stuff
      }

Destructors

  • in C++ every class also has a destructor, which is called when it is deleted
  • its name is the class name with \textasciitilde in front of it
      ~boid()
      {
      	//destroy stuff
      }

Destructors

  • whenever you have a pointer in your class you should delete it in your destructor
     class boid{
         foo *f;
 
         boid(){
           	f = new foo();
         }
 
        ~boid(){
      	  delete f;
        }
     }

NULL

  • In Java a class that isn’t initialised is automatically set to null
  • (this is actually a pointer with address 0)
  • in C++ this doesn’t happen, its set to a random number
  • if you have a pointer that you aren’t initialising straight away you should always set it to NULL (NB in C++ it is in capitals)
     boid *b = NULL;
Create PDF    Send article as PDF to