Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Data Structures Pointers and Dynamic Arrays by Dr.

Fatima Bahjat

Pointers in C++
A pointer variable is a variable that stores the address where another object resides. (A pointer is the
memory address of a variable). It is the fundamental mechanism used in many data structures. For
instance, to store a list of items, we could use a contiguous array, but insertion into the middle of the
contiguous array requires relocation of many items. Rather than store the collection in an array, it is
common to store each item in a separate, noncontiguous piece of memory, which is allocated as the
program runs. Along with each object is a link to the next object. This link is a pointer variable,
because it stores a memory location of another object.

The complete declaration of a pointer variable must contain three items, as shown here in this
example:

A variable that is a pointer to other variables of type Type_Name is declared in the same way that you
declare a variable of type Type_Name, except that you place an asterisk at the beginning of the
variable name.

Syntax:

Type_Name *var_name1;

Examples:

int *cursor;

char *c1_ptr, *c2_ptr;

a pointer variable is of no use unless there is something for it to point to. For example, consider these
two declarations:

int *example_ptr;

int i;

We can make example_ptr contain the address of i by using the & operator shown here:

example_ptr = &i;

This statement puts the address of i into the pointer variable example_ptr.

DS-3rd class-1st semester (2021-2022) Page 1 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

The & operator, called the address operator, provides the address of a variable; the & operator for
instance, &i is “the address of the integer variable i.”. So the assignment statement places “the
address of i” into example_ptr. Or we could simply say that example_ptr now “points to” i. After the
assignment, you have two ways to refer to i: You can call it i, or the * operator you can call it “the
variable pointed to by example_ptr.” In C++ “the variable pointed to by example_ptr” is written
*example_ptr. This is the same asterisk notation that we used to declare *example_ptr, but now it
has yet another meaning. When the asterisk is used in this way, it is called the dereferencing
operator, and the pointer variable is said to be dereferenced. For example:

You can copy the value of one pointer variable to another with the usual assignment operator. For
example:

When dealing with pointer variables, there is a critical distinction between a pointer variable (such as
p1) and the thing it points to (such as *p1). Do not confuse the meaning of these two assignment
statements:

p2 = p1; versus *p2 = *p1;

As we have seen, p2 = p1 means “make p2 point to the same variable that p1 is already pointing to.”
On the other hand, the inclusion of the dereferencing asterisks in *p2 = *p1 gives the statement quite
DS-3rd class-1st semester (2021-2022) Page 2 of 10
Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

a different meaning. The new meaning is to “copy the value from the variable that p1 points to, to the
variable that p2 points to.” Here is an example, which starts by declaring two integers and two
pointers to integers:

Once the two pointers are initialized, we can see the effect of an assignment statement:

The assignment statement has copied the value 42 from the variable that p1 points to, to the variable
that p2 points to. In effect, j has changed its contents, but the pointers themselves still point to
separate memory locations.

Dynamic Variables and the new Operator

Pointers may point to ordinary variables, such as i in our previous example. But the real power of
pointers arises when pointers are used with special kinds of variables called dynamically allocated
variables, or more simply, dynamic variables. Dynamic variables are like ordinary variables, with two
important differences:

1. Dynamic variables are not declared. A program may use many dynamic variables, but the
dynamic variables never appear in any declaration the way an ordinary variable does.
Moreover, a dynamic variable has no identifier (such as the identifiers i and j that are used in
our examples).
2. Dynamic variables are created during the execution of a program. Only at that time does a
dynamic variable come into existence.

To create a dynamic variable while a program is running, C++ programs use an operator called new, as
shown here:

The creation of new dynamic variables is called memory allocation and the memory is dynamic
memory
DS-3rd class-1st semester (2021-2022) Page 3 of 10
Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

Using new to Allocate Dynamic Arrays

We have seen the new operator allocate one dynamic variable at a time. But in fact, new can allocate
an entire array at once. The number of array components is listed in square brackets, immediately
after the component data type, as shown here:

When new allocates an entire array, it actually returns a pointer to the first component of the array.

The array version of new is particularly useful because the number of array components can be
calculated while the program is running. Therefore, the number of components can depend on factors
such as user input.

The following statement will place 3.14 in the [9] component of the new array:

Here is another example, which allocates a new int array:

DS-3rd class-1st semester (2021-2022) Page 4 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

The new Operator

The new operator allocates memory for a dynamic variable of a specified type and returns a pointer to
the newly allocated memory. For example, the following code allocates a new dynamic integer
variable and sets p to point to this new variable:

int *p;
p = new int;

If the dynamic variable is an object of a class, then the default constructor will be called to initialize
the new class instance. A different constructor will be called if you place the constructor’s arguments
after the type name in the new statement. For example:

The new operator can also allocate a dynamic array of components, returning a pointer to the first
element. The size of the array is specified in square brackets after the data type of the components:

If
the data type of the array component is a class, then the default constructor is used to initialize all

DS-3rd class-1st semester (2021-2022) Page 5 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

components of the dynamic array. There is no mechanism to use a different constructor on the array
components.

The Heap and the bad_alloc Exception

When new allocates a dynamic variable or dynamic array, the memory comes the heap from a
location called the program’s heap (also called the free store).

The new operator usually indicates failure by throwing an exception called the bad_alloc exception.
Normally, an exception causes an error message to be printed and the program to halt. (Older C++
implementations use a different mechanism for new failure.)

We clearly document which functions use new so that experienced programmers can deal with the
failure in their own manner.

The delete Operator

The size of the heap varies from one computer to another. It could be just a few thousand bytes or
more than a billion. Small programs are not likely to use all of the heap. However, even with small
programs, it is an efficient practice to release any heap memory that is no longer needed. If your
program no longer needs a dynamic variable, the memory used by that dynamic variable can be
returned to the heap where it can be reused for more dynamic variables. In C++, the delete operator is
used to return the memory of a dynamic variable back to the heap. The delete operator is called by
writing the word delete, followed by the pointer variable.

Using the delete operation is called freeing or releasing memory.

The delete Operator

The delete operator frees memory that has been used for dynamic variables. The memory is returned
to the heap, where it can be reused at a later time. For example, the following code allocates an
integer dynamic variable, and frees the memory when it is no longer needed:

The delete operator can also free a dynamic array of components. All of the array’s memory is
returned to the heap, where it can be reused. To free an entire array, the array brackets [ ] are placed
after the word delete, as shown here:

DS-3rd class-1st semester (2021-2022) Page 6 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

The array size does not need to be specified with the delete operator.

Pointers and Arrays as Parameters

Value Parameters That Are Pointers

When a value parameter is a pointer, the function may change the value in the location that the
pointer points to. The actual argument in the calling program will still point to the same location, but
that location will have a new value.

Syntax in the parameter list:


Type_Name* var_name

Example

The parameter is a value parameter because the reference symbol & does not appear.

DS-3rd class-1st semester (2021-2022) Page 7 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

Array Parameters

A parameter that is an array is indicated by placing [ ] after the parameter name, as shown here:

Syntax in the parameter list:


Type_Name var_name[ ]
Example

There is usually a separate size_t parameter to indicate the size of the array. Any changes that the
function makes to the components of the array do affect the actual argument.

The function’s prototype is as follows:

The size of the array is not needed inside the brackets, but usually there is another parameter (such as
size_t n) that indicates the size of the array.

DS-3rd class-1st semester (2021-2022) Page 8 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

Const Parameters That Are Pointers or Arrays

A parameter that is a pointer or array may include the const keyword, as shown here:

Syntax in the parameter list:


const Type_Name* var_name
const Type_Name var_name[ ]

The functions may examine the item that is pointed to (or the array), but changing the item (or array)
is forbidden.

Reference Parameters That Are Pointers

Sometimes a function will actually change a pointer parameter so that the pointer points to a new
location, and the programmer needs the change to affect the actual parameter. This is the only
situation in which a reference parameter will be a pointer.

Syntax in the parameter list:


Type_Name*& var_name

DS-3rd class-1st semester (2021-2022) Page 9 of 10


Data Structures Pointers and Dynamic Arrays by Dr. Fatima Bahjat

HW1/ from book figure 4.7 in pages 173-175 “a program for dynamic array”.

HW2/ search for the different using and placing of the keyword const with an example for each use.

HW3/ What are the differences between static variable and dynamic variable?

DS-3rd class-1st semester (2021-2022) Page 10 of 10

You might also like