Pointers and the Free Store

Finding Addresses Explicitly


// address.cpp -- using the & operator to find addresses
#include <iostream>
using namespace std;
int main()
{
   int donuts = 6;
   double cups = 4.5;

   cout << "donuts value = " << donuts;
   cout << " and donuts address = " << &donuts << "\n";
   cout << "cups value = " << cups;
   cout << " and cups address = " << &cups << "\n";
   return 0;
}

OUTPUT:
donuts value = 6 and donuts address = 006AFDF4
cups value = 4.5 and cups address = 006AFDEC

What Pointers Are

Declaring and Initializing Pointers

Pointers and Numbers

Allocating Memory with New

Freeing Memory with Delete

Creating Dynamic Arrays

Static Binding

Dynamic Binding

Creating Dynamic Arrays

Rules when using new and delete

  1. Don't use delete to free memory that new didn't allocate.
  2. Don't use delete to free the same block of memory twice in succession.
  3. Use delete [] if you used new [] to allocate an array.
  4. Use delete (no brackets) if you used new to allocate a single entity.
  5. It's safe to apply delete to the null pointer (nothing happens).

Using a Dynamic Array

Pointers, Arrays, and Pointer Arithmetic

Pointers and Strings

Links

http://www.thecodeproject.com/cpp/pointers.asp

http://www.gamedev.net/reference/articles/article1697.asp