# MATERIAL ON POINTERS AND DYNAMIC MEMORY IN C++

written by David Metcalf and Huan Luo. Modified by Elena Machkasova.

## INTRODUCTION TO POINTERS

```A POINTER is a C++ variable whose value is an address in memory.
We say that a pointer p "points to" variable x if its value is the address
of x.```

## DECLARING POINTERS

```A pointer is declared based on the type of the variable it is supposed to
point to.
A pointer p which points to an integer variable is declared like this:
int *p;
The "*" indicates that variable p is a pointer.
The * is part of the variable type: variable p is a variable of type "int
*"
If several pointers are declared in the same line, each must be preceded
by a *:
int *p, *q, *r;
```

## SETTING POINTERS

```
Normally, a variable name in an expression stands for the VALUE of that
variable
example:  a = b  4;
should be read "set the value of a to be the value of b plus 4".

The & (ampersand) operator is used to ask for the ADDRESS of a variable.
example:  &x

will stand for the memory address in which variable x is stored.

To set a pointer to point to a particular variable, use the & operator:
p = &x;
sets the value of pointer variable p to be the address of x.

To set a pointer to point to nothing, set to a value 0 or,
alternatively, to the reserved value NULL:
p = 0;
or
p = NULL;
Note: NULL has been used in C. In C++ one may still use NULL, but

Any pointer which is not currently pointing to a specific object should be
set to 0 or NULL.  Pointers should usually be initialized to 0.
Note: Only pointers, but no other variables, may be set to NULL.
```

## USING POINTERS

```
Assume pointer p is a pointer pointing to variable x.
We use the * operator (called the DEREFERENCING OPERATOR) to access x from
p.
The expression *p can be read, "The value at the memory location pointed
to by p"

examples:
*p = 7;         // sets the value of x to 7 (assuming p points to
x)
*p = *p;        // adds 3 to the value of x

WARNING - NEVER DEREFERENCE A POINTER WHOSE VALUE IS 0 OR NULL

```

# DYNAMIC MEMORY ALLOCATION - INTRODUCTION

```
When one defines a variable in C, the compiler allocates memory for that
variable
automatically when the program begins running.  A Data structure declared
this way
is called a STATIC STRUCTURE because it never changes size for the
duration of the
run of the program.

When you want to allocate memory while the program is running, you must do
it using DYNAMIC
MEMORY ALLOCATION.  A structure created this way is called a DYNAMIC
STRUCTURE.
```

## THE "NEW" COMMAND

```In C, dynamic memory allocation is done using the "new" command.
The new command takes a type as an argument, allocates enough memory to
hold a variable
of that type, and returns the address of the newly allocated memory.
This address should then be stored in a pointer.
The format is:
MyTypePtr = new MyType;
where MyType is some type (e.g. int, float, or perhaps a structure or
class),
and MyTypePtr is a pointer to a MyType.

example:
int *intPtr;
intPtr = new int;       // dynamically allocates enough memory for
// a single int,
// and stores the address in intPtr.

myClass *classPtr;
classPtr = new myClass; // dynamically allocates enough memory for
// a myClass object,
// and stores the address in classPtr.
// Note that this automatically invokes
// class constructor.

To initialize the memory being allocated, an initial value can be included
as an argument:
int *intPtr;
intPtr = new int(6);    // dynamically allocates enough memory for
// a single int,
// and initializes it to the value 6.
```

## DEALLOCATING DYNAMICALLY ALLOCATED MEMORY

```To deallocate memory which has been allocated dynamically, use the
"delete" command:
delete intPtr;          // Deallocates memory pointed to by
// intPtr.
delete classPtr;        // Deallocates myClass object pointed to
// by classPtr.
// Note that this automatically invokes
// class destructor.
Note that "delete intPtr" does NOT delete the variable intPtr itself - it
deallocates (i.e. makes available for future allocation) the memory whose

*WARNING*  NEVER TRY TO DELETE MEMORY WHICH WAS NOT ALLOCATED USING THE
"NEW" COMMAND
You CANNOT delete memory which was allocated statically
```

## DYNAMICALLY ALLOCATING ARRAYS

```
To dynamically allocate an array, specify the array size in square
brackets [] after the type:
int *intArrayPtr;
intArrayPtr = new int[20];      // dynamically allocate enough
// memory for an array of 10 ints.
// IntArrayPtr now points to the
// 1st element of the array.

To delete a dynamically allocated array, you must include a pair of empty
square brackets
in the delete statement, just after the delete command:
delete [] intArrayPtr;

These brackets are easily forgotten, so watch out for them.

```