You are on page 1of 27

Center for Information and Communication Technology

LECTURE 09

POINTERS
AND
DYNAMIC MEMORY MANAGEMENT

1
Center for Information and Communication Technology

POINTER
Pointer variables are also known as pointers.
You can use a pointer to reference the address of an array, an object, or any
variable.
Pointer is one of the most powerful features in C++. It is the heart and soul of
the C++ programming language.
Many of the C++ language features and libraries are built using pointers.
To see why pointers are needed, let us consider writing a program that
processes an unspecified number of integers.
You would use an array to store the integers. But how do you create the array if
you don’t know its size? The size may change as you add or remove integers.
To deal with this, your program needs the ability to allocate and release the
memory for the integers on the fly at runtime.
This can be accomplished using pointers.
2
Center for Information and Communication Technology

POINTER
A pointer variable holds the memory address.
Through the pointer, you can use the dereference operator * to access the actual
value at a specific memory location.
Pointer variables, simply called pointers, are declared to hold memory addresses
as their values.
Normally, a variable contains a data value—e.g., an integer, a floating-point value,
and a character.
However, a pointer contains the memory address of a variable that in turn
contains a data value.
As shown in Figure 11.1, pointer pCount contains the memory address for
variable count.
Each byte of memory has a unique address.
A variable’s address is the address of the first byte allocated to that variable.
3
Center for Information and Communication Technology

POINTER
Suppose three variables count, status, and letter are
declared as follows
int count = 5;
short status = 2;
char letter = 'A';
string s("ABC");

4
Center for Information and Communication Technology

POINTER
As shown in Figure 11.1, variable count is declared as an int
type which contains four bytes, variable status is declared as a
short type which contains two bytes, and variable letter is
declared as a char type which contains one byte.
Note that the ASCII code for 'A' is hex 55.
Variables is declared as a string type whose memory size may
change, depending on the number of the characters in the
string, but the memory address for the string is fixed, once
string is declared.
Like any other variables, pointers must be declared before they
can be used
5
Center for Information and Communication Technology

POINTER
To declare a pointer, use the following syntax:
dataType* pVarName;
Each variable being declared as a pointer must be preceded by an asterisk (*).
For example, the following statements declare pointers named pCount, pStatus,
and pLetter, which can point to an int variable, a short variable, a char variable,
and a string, respectively.
int* pCount;
short* pStatus;
char* pLetter;
string* pString;
You can now assign the address of a variable to a pointer. For example, the
following code assigns the address of variable count to pCount
pCount = &count;
6
Center for Information and Communication Technology

POINTER
The ampersand (&) symbol is called the address operator
when placed in front of a variable.
It is a unary operator that returns the variable’s address.
 You may pronounce &count as the address of count.

7
Center for Information and Communication Technology

pCount contains the memory


address of variable count

int count = 5;
short status = 2;
char letter = 'A';
string s = "ABC";
int* pCount = &count;
short* pStatus = &status;
char* pLetter = &letter;
string* pString = &s;
pCount = &count;
&: address operator
&count means the address of count
*: dereferencing operator
*pCount means the value pointed by pCount is assigned
to v.

8
Center for Information and Communication Technology

Using const with Pointers


A constant pointer points to a constant memory location,
but the actual value in the memory location can be changed.
You have learned how to declare a constant using the const
keyword. Once it is declared, a constant cannot be changed.
You can declare a constant pointer. For example:
double radius = 5;
double* const p = &radius;
Here p is a constant pointer. It must be declared and
initialized in the same statement
9
Center for Information and Communication Technology

Using const with Pointers


You cannot assign a new address to p later. Though p is a
constant, the data pointed to by p is not constant.
You can change it. For example, the following statement
changes radius to 10:
*p = 10;
Can you declare that dereferenced data be constant? Yes.
You can add the const keyword in front of the data type,
as follows:

10
Center for Information and Communication Technology

Using const with Pointers


In this case, the pointer is a constant, and the data
pointed to by the pointer is also a constant.

11
Center for Information and Communication Technology

Arrays and Pointers


A C++ array name is actually a constant pointer to the first element in the
array.
An array without a bracket and a subscript actually represents the starting
address of the array.
In this sense, an array is essentially a pointer. Suppose you declare an array
of int values as follows:
int list[6] = {11, 12, 13, 14, 15, 16};
The following statement displays the starting address of the array:
cout << "The starting address of the array is " << list << endl;
Figure below shows the array in the memory.
C++ allows you to access the elements in the array using the dereference
operator. To access the first element, use *list
12
Center for Information and Communication Technology

Arrays and Pointers

Array list points to the first element in the array

13
Center for Information and Communication Technology

Arrays and Pointers


Other elements can be accessed using *(list + 1), *(list +
2), *(list + 3), *(list + 4), and *(list + 5).
An integer may be added to or subtracted from a pointer.
The pointer is incremented or decremented by that
integer times the size of the element to which the pointer
points.
Array list points to the starting address of the array.
Suppose this address is 1000. Will list + 1 be 1001? No. It
is 1000 + sizeof(int). Why?
14
Center for Information and Communication Technology

Why? Continue…..
Since list is declared as an array of int elements, C++
automatically calculates the address for the next element
by adding sizeof(int).
Recall that sizeof(type) function returns the size of a data
type
The size of each data type is machine dependent.
On Windows, the size of the int type is usually 4. So, no
matter how big each element of the list is, list + 1 points to
the second element of the list, and list + 3 points to the
third, and so on.
15
Center for Information and Communication Technology

ArrayPointer.cpp
address: 0013FF4C value: 11 value: 11
address: 0013FF50 value: 12 value: 12
address: 0013FF54 value: 13 value: 13
address: 0013FF58 value: 14 value: 14
address: 0013FF5C value: 15 value: 15
address: 0013FF60 value: 16 value: 16

16
Center for Information and Communication Technology

Arrays and Pointers


As shown in the sample output, the address of the array list is
0013FF4C.
So (list +1) is actually 0013FF4C + 4, and (list + 2) is 0013FF4C + 2 * 4
(line 9).
The array elements are accessed using pointer dereference *(list + 1).
To accesses the elements via index using list[i], which is equivalent to
*(list + i).
NOTE
*(list + 1) is different from *list + 1. The dereference operator (*) has
precedence over +. So, *list + 1 adds 1 with the value of the first element
in the array, while *(list + 1) dereferences the element at address (list +
1) in the array.
17
Center for Information and Communication Technology

Arrays and Pointers


Pointers can be compared using relational operators
(==, !=, <, <=, >, >=) to determine their order.
Arrays and pointers form a close relationship.
A pointer for an array can be used just like an array. You
can even use pointer with index as shown below

18
Center for Information and Communication Technology

address: 0013FF4C value: 11 value: 11 value: 11 value: 11


address: 0013FF50 value: 12 value: 12 value: 12 value: 12
address: 0013FF54 value: 13 value: 13 value: 13 value: 13
address: 0013FF58 value: 14 value: 14 value: 14 value: 14
address: 0013FF5C value: 15 value: 15 value: 15 value: 15
address: 0013FF60 value: 16 value: 16 value: 16 value: 16

19
Center for Information and Communication Technology

Arrays and Pointers


Line 7 declares an int pointer p assigned with the address of the array.
int* p = list;
Note that we do not use the address operator (&) to assign the address
of the array to the pointer, because the name of the array is already the
starting address of the array. This line is equivalent to
int* p = &list[0];
Here, &list[0] represents the address of list[0].
You can access an element using array syntax list[i] as well as pointer
syntax *(list + i).
When a pointer such as p points to an array, you can use either pointer
syntax or the array syntax to access an element in the array i.e., *(p + i)
or p[i]
20
Center for Information and Communication Technology

Arrays and Pointers


You can use array syntax or pointer syntax to access
arrays, whichever is convenient. However, there is one
difference between arrays and pointers.
Once an array is declared, you cannot change its address.
For example, the following statement is illegal:
int list1[10], list2[10];
list1 = list2; // Wrong
An array name is actually treated as a constant pointer in
C++.
21
Center for Information and Communication Technology

Enumeration
Enumeration is a user defined datatype in C/C++
language.
It is used to assign names to the integral constants which
makes a program easy to read and maintain.
The keyword “enum” is used to declare an enumeration.
The following is the syntax of enums.
enum enum_name {const1, const2, ....... };

22
Center for Information and Communication Technology

enum_name − Any name given by user.


const1, const2 − These are values of type flag.
The enum keyword is also used to define the variables of
enum type.
There are two ways to define the variables of enum type
as follows −
enum colors{red, black};
enum suit{heart, diamond=8, spade=3, club};

23
Center for Information and Communication Technology

#include <iostream>
using namespace std;
enum colors{red=5, black};
enum suit{heart, diamond=8, spade=3, club};
int main() {
cout <<"The value of enum color “ <<red<<","<<black;
cout <<"\nThe default value of enum suit”;
cout<<heart<<","<<diamond<<","<<spade<<","<<club;
return 0;
}
24
Center for Information and Communication Technology

Output
The value of enum color : 5,6
The default value of enum suit : 0,8,3,4
In the above program, two enums are declared as color
and suit outside the main() function.
enum colors{red=5, black};
enum suit{heart, diamond=8, spade=3, club};
In the main() function, the values of enum elements are
printed

25
Center for Information and Communication Technology

typedef
What is typedef in C and C++?
As the name itself suggests, typedef stands for “type
definition”.
typedef is nothing but a way to assign a new name to a
pre-existing data type.
In other words, typedef is basically a reserved keyword
that we use in order to create an alias name for a specific
data type.

26
Center for Information and Communication Technology

typedef
A simple syntax to define a new type using typedef is;-
typedef type newname;
For example, the following tells the compiler that feet is
another name for int
typedef int feet;
Now declaration is perfectly legal and creates an integer
variable called distance
feet distance;

27

You might also like