CSCI101

An Introduction to Programming using C++

Chapter 12

 

Pointers and Dynamic Arrays

 

1 Pointers

 

2 Dynamic Arrays

 

3 Classes and Dynamic Arrays        omit this section

 

Pointers

 

Today we will be referring to this document:

 

http://www.annedawson.com/pointers.htm

 

 

Contents of pointers.htm

*    Introduction to pointers

*    The use of pointers in C/C++

*    Pointers to simple variables

*    Pointer declarations

*    Pointer initializations

*    Using a pointer to change the value of a variable

*    Pointer Arithmetic

 

http://www.annedawson.com/pointers.htm

 

Pointer Arithmetic

Summary

 

int num = 10;

int *ptr1; // ptr1 is a pointer

// to an integer

ptr1 = # // ptr1 takes the

// ADDRESS of num

 

// the & is known as the

// "address of operator"

 

*ptr1 = 20;

// changes the contents

//of num to 20

// the * is known as the

// indirection operator

// a.k.a. dereferencing operator

// but only when it occurs in a

// statement NOT in a declaration

// of a pointer

 

Variables with no name

Since a pointer can be used to refer to a variable, a C/C++ program can manipulate variables even if the variables have no identifying name (like num). How? ...

 

The new operator

 

The new operator creates variables of no name.

 

 

int *ptr1; // ptr1 is a pointer to an int

ptr1 = new int;// an address of an

// unidentified int is

// assigned to ptr1

cin >> *ptr1; // inputs a value (at the

// address held by ptr)

// from the keyboard

*ptr1 = *ptr1 + 10; // adds 10 to

// the value at the address

// held by ptr1

cout << *ptr1; // outputs the value

// in the address stored

// by ptr1

 

 

 

The new operator

The new operator can be used to create variables of no name.

These nameless variables are referred to via pointers.

The new operator produces a new nameless variable and returns an address that points to this new variable.

 

Example Program
The new operator and nameless variables

pointers07.cpp

 

Dynamic Variables

A dynamic variable is a variable which is created using the new operator.

 

With early compilers, if there is not enough memory to create the new variable, then new returns a special pointer value named NULL (which actually is integer 0).

 

Newer compilers exit the code is there is not enough memory to create a new variable.

 

 

Basic Memory Management - The Heap

 

A special area of memory known as the heap is reserved for dynamic variables.

 

If all of the heap is consumed by the dynamic variables in your code, additional calls to new will fail and NULL will be returned.

To use NULL in your code you must include stddef.h

 

int *p;

p = new int;

if (p == NULL) // needs stddef.h

{

cout << "Not enough memory";

exit(1); // needs stdlib.h

}

 

The Heap

The size of the heap varies from one implementation of C/C++ to another.

 

It is usually large enough for most programs.

 

BUT you should free up heap memory when you dont need it any more. . .

 

 

The delete operator

The delete operator eliminates a dynamic variable and returns the memory that it occupied to the heap.

 

The use of the delete operator

 

int * ptr1;

ptr1 = new int;

*ptr1 = 10;

cout << *ptr1;

delete ptr1;

 

 

Example Program:The delete operator

pointers08.cpp

 

 

Warning

If you refer to a pointer in a program after the pointer has been deleted, the address contained by the pointer will be a garbage value, hence could be pointing anywhere.

 

 

Warning!

If you write data to a random address using a pointer, you may be overwriting an area of memory of a currently running program, such as the operating system. In such a case unpredictable behavior will occur.

 

 

A pointer which contains a garbage address is called a dangling pointer.

 

Dangling pointers

Before you apply the dereferencing (aka indirection) operator (*) to a pointer you should be certain that the pointer variable points to some variable. Otherwise - the result is unpredictable.

 

To avoid the possibility of a dangling pointer:

Once you've used delete on a pointer,

assign NULL to the pointer:

 

    delete ptr1;

    ptr1 = NULL;

 

 

To avoid the possibility of a dangling pointer:

Once NULL has been assigned to a pointer, later in your code you may want to check if the pointer contains this value, otherwise you can assume it contains a valid address.

 

if (ptr1 == NULL) ...

 

A good programming tip

You can define a pointer type name so that pointer variables can be declared without having to use the * each time:

typedef int * intptr;

intptr ptr1, ptr2;

// instead of: int *ptr1, *ptr2;

 

Dynamic Variables

A dynamic variable is a variable which is created using the new operator.

 

Dynamic Array Variables

It is also possible to have dynamic array

variables.

 

A dynamic array is an array whose size is not specified when you write the program, but is determined while the program is running.

 

 

Note

 

The name of any array is the address of the first element.

 

 

An array variable IS AN ADDRESS.

 

A pointer IS AN ADDRESS.

 

So, in C/C++ an array variable is actually a pointer variable that points to the first indexed variable (element) of the array.

 

int my_array[5];

int * intptr;

intptr = my_array; //quite legal!

 

// However the reverse is illegal:

// my_array = intptr;//dont do it!

 

How to Create a Dynamic Array

 

1) Define a pointer type :

          typedef int *IntPtr;

2) Declare a pointer variable:

          IntPtr ptr1;

3) Call new

          ptr1 = new int[array_size];

 

How to Use a Dynamic Array

 

1) Check the call to new was successful:

     if (ptr1 == NULL)

 

2) Use just like an ordinary array

     ptr1[0] is the first element of the array

 

3) Call delete when you are finished using it

delete [] ptr1;

 

 

Textbook Example Program:Dynamic Arrays
12-05.cpp

 

Textbook Example Program:Two-Dimensional Dynamic Array
12-06.cpp

 

 

This Presentation uses the following files:

 

               5th Ed 4th Ed 3rd Ed 2nd Ed

pointers07.cpp   ---     ---    ---    ---

pointers08.cpp   ---     ---    ---    ---

12-05.cpp       p633     p717  p697   p674

12-06.cpp       p638     p723   ---    ---

 

End of

Pointers and Dynamic Arrays

 

Last updated: Saturday 11th February 2006, 13:06 PT, AHD