Professional Documents
Culture Documents
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
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
10
Center for Information and Communication Technology
11
Center for Information and Communication Technology
13
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
18
Center for Information and Communication Technology
19
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
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