SlideShare una empresa de Scribd logo
1 de 62
CSC2110 – Data Structures/Algorithms
By
Faisal Shahzad Khan
BSCS UET Texila
03364180007
Facebook.com/fasii.07
Pointers
 Pointers
 Powerful feature of the C++ language
 One of the most difficult to master
 Essential for construction of interesting data structures
2Faisal Shahzad Khan
Addresses and Pointers
 C++ allows two ways of accessing variables
 Name (C++ keeps track of the address of the first
location allocated to the variable)
 Address/Pointer
 Symbol & gets the address of the variable that
follows it
 Addresses/Pointers can be displayed by the cout
statement
 Addresses displayed in HEXADECIMAL
Faisal Shahzad Khan 3
Example
#include <iostream.h>
void main( )
{
int data = 100;
float value = 56.47;
cout << data << &data << endl;
cout << value << &value << endl;
}
Output:
100 FFF4
56.47 FFF0
Faisal Shahzad Khan 4
56.47
100
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
value
data
Pointer Variables
 The pointer data type
 A data type for containing an address rather than a data
value
 Integral, similar to int
 Size is the number of bytes in which the target computer
stores a memory address
 Provides indirect access to values
Faisal Shahzad Khan 5
Declaration of Pointer Variables
 A pointer variable is declared by:
dataType *pointerVarName;
 The pointer variable pointerVarName is used to point to
a value of type dataType
 The * before the pointerVarName indicates that this is a
pointer variable, not a regular variable
 The * is not a part of the pointer variable name
Faisal Shahzad Khan 6
Declaration of Pointer Variables
(Cont ..)
 Example
int *ptr1;
float *ptr2;
 ptr1 is a pointer to an int value i.e., it can have the
address of the memory location (or the first of more
than one memory locations) allocated to an int
value
 ptr2 is a pointer to a float value i.e., it can have
the address of the memory location (or the first of
more than one memory locations) allocated to a
float value
Faisal Shahzad Khan 7
Declaration of Pointer Variables
(Cont ..)
 Whitespace doesn’t matter and each of the following
will declare ptr as a pointer (to a float) variable and
data as a float variable
float *ptr, data;
float* ptr, data;
float (*ptr), data;
float data, *ptr;
Faisal Shahzad Khan 8
Assignment of Pointer Variables
 A pointer variable has to be assigned a valid
memory address before it can be used in the
program
 Example:
float data = 50.8;
float *ptr;
ptr = &data;
 This will assign the address of the memory location
allocated for the floating point variable data to the
pointer variable ptr. This is OK, since the variable
data has already been allocated some memory space
having a valid address
Faisal Shahzad Khan 9
Assignment of Pointer Variables
(Cont ..)
Faisal Shahzad Khan 10
float data = 50.8;
float *ptr;
ptr = &data; 50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
data
Assignment of Pointer Variables
(Cont ..)
Faisal Shahzad Khan 11
float data = 50.8;
float *ptr;
ptr = &data; 50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
Assignment of Pointer Variables
(Cont ..)
Faisal Shahzad Khan 12
float data = 50.8;
float *ptr;
ptr = &data;
FFF4
50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
Assignment of Pointer Variables
(Cont ..)
 Don’t try to assign a specific integer value to a
pointer variable since it can be disastrous
float *ptr;
ptr = 120;
Faisal Shahzad Khan 13
• You cannot assign the address of one type of
variable to a pointer variable of another type
even though they are both integrals
int data = 50;
float *ptr;
ptr = &data;
Initializing pointers
 A pointer can be initialized during declaration by
assigning it the address of an existing variable
float data = 50.8;
float *ptr = &data;
 If a pointer is not initialized during declaration, it
is wise to give it a NULL (0) value
int *ip = 0;
float *fp = NULL;
Faisal Shahzad Khan 14
The NULL pointer
 The NULL pointer is a valid address for any data type.
 But NULL is not memory address 0.
 It is an error to dereference a pointer whose value is
NULL.
 Such an error may cause your program to crash, or
behave erratically.
 It is the programmer’s job to check for this.
Faisal Shahzad Khan 15
Dereferencing
 Dereferencing – Using a pointer variable to access
the value stored at the location pointed by the
variable
 Provide indirect access to values and also called
indirection
 Done by using the dereferencing operator * in front
of a pointer variable
 Unary operator
 Highest precedence
Faisal Shahzad Khan 16
Dereferencing (Cont ..)
 Example:
float data = 50.8;
float *ptr;
ptr = &data;
cout << *ptr;
 Once the pointer variable ptr has been declared,
*ptr represents the value pointed to by ptr (or
the value located at the address specified by ptr)
and may be treated like any other variable of
float type
Faisal Shahzad Khan 17
Dereferencing (Cont ..)
 The dereferencing operator * can also be used in
assignments.
*ptr = 200;
 Make sure that ptr has been properly initialized
Faisal Shahzad Khan 18
Dereferencing Example
Faisal Shahzad Khan 19
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
Output:
FFF4
50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
Dereferencing Example (Cont ..)
Faisal Shahzad Khan 20
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
Output:
FFF4 50.80
FFF4
50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
Dereferencing Example (Cont ..)
Faisal Shahzad Khan 21
FFF4
27.4
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
Output:
Dereferencing Example (Cont ..)
Faisal Shahzad Khan 22
FFF4
27.4
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
Output:
27.4
Dereferencing Example (Cont ..)
Faisal Shahzad Khan 23
FFF4
27.4
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
#include <iostream.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
cout << ptr << *ptr << endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
}
Output:
27.4
Operations on Pointer Variables
 Assignment – the value of one pointer variable can
be assigned to another pointer variable of the same
type
 Relational operations - two pointer variables of the
same type can be compared for equality, and so on
 Some limited arithmetic operations
 integer values can be added to and subtracted from a
pointer variable
 value of one pointer variable can be subtracted from
another pointer variable
Faisal Shahzad Khan 24
Pointers to arrays
 A pointer variable can be used to access the
elements of an array of the same type.
int gradeList[8] = {92,85,75,88,79,54,34,96};
int *myGrades = gradeList;
cout << gradeList[1];
cout << *myGrades;
cout << *(myGrades + 2);
cout << myGrades[3];
 Note that the array name gradeList acts like the
pointer variable myGrades.
Faisal Shahzad Khan 25
Faisal Shahzad Khan 26
Dynamic Memory Allocation
Types of Program Data
 Static Data: Memory allocation exists
throughout execution of program
 Automatic Data: Automatically created at
function entry, resides in activation frame of the
function, and is destroyed when returning from
function
 Dynamic Data: Explicitly allocated and
deallocated during program execution by C++
instructions written by programmer
Faisal Shahzad Khan 27
Allocation of Memory
 Static Allocation: Allocation of memory space at
compile time.
 Dynamic Allocation: Allocation of memory space at
run time.
Faisal Shahzad Khan 28
Dynamic memory allocation
 Dynamic allocation is useful when
 arrays need to be created whose extent is not known until
run time
 complex structures of unknown size and/or shape need to
be constructed as the program runs
 objects need to be created and the constructor arguments
are not known until run time
Faisal Shahzad Khan 29
Dynamic memory allocation
 Pointers need to be used for dynamic allocation of
memory
 Use the operator new to dynamically allocate space
 Use the operator delete to later free this space
Faisal Shahzad Khan 30
The new operator
 If memory is available, the new operator allocates
memory space for the requested object/array, and returns
a pointer to (address of) the memory allocated.
 If sufficient memory is not available, the new operator
returns NULL.
 The dynamically allocated object/array exists until the
delete operator destroys it.
Faisal Shahzad Khan 31
The delete operator
 The delete operator deallocates the object or array
currently pointed to by the pointer which was
previously allocated at run-time by the new operator.
 the freed memory space is returned to Heap
 the pointer is then considered unassigned
 If the value of the pointer is NULL there is no effect.
Faisal Shahzad Khan 32
Example
Faisal Shahzad Khan 33
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptrint *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
Faisal Shahzad Khan 34
Example (Cont ..)
int *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
0EC4
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptr
Faisal Shahzad Khan 35
Example (Cont ..)
int *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
0EC4
22
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptr
Faisal Shahzad Khan 36
Example (Cont ..)
int *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
0EC4
22
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptr
Output:
22
Faisal Shahzad Khan 37
Example (Cont ..)
int *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
?
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptr
Faisal Shahzad Khan 38
Example (Cont ..)
int *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
0
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptr
Dynamic allocation and
deallocation of arrays
 Use the [IntExp] on the new statement to create an
array of objects instead of a single instance.
 On the delete statement use [] to indicate that an
array of objects is to be deallocated.
Faisal Shahzad Khan 39
Example of dynamic array
allocation
Faisal Shahzad Khan 40
int* grades = NULL;
int numberOfGrades;
cout << "Enter the number of grades: ";
cin >> numberOfGrades;
grades = new int[numberOfGrades];
for (int i = 0; i < numberOfGrades; i++)
cin >> grades[i];
for (int j = 0; j < numberOfGrades; j++)
cout << grades[j] << " ";
delete [] grades;
grades = NULL;
Dynamic allocation of 2D arrays
 A two dimensional array is really an array of arrays
(rows).
 To dynamically declare a two dimensional array of
int type, you need to declare a pointer to a pointer
as:
int **matrix;
Faisal Shahzad Khan 41
Dynamic allocation of 2D arrays
(Cont ..)
 To allocate space for the 2D array with r rows
and c columns:
 You first allocate the array of pointers which will
point to the arrays (rows)
matrix = new int*[r];
 This creates space for r addresses; each being a
pointer to an int.
 Then you need to allocate the space for the 1D
arrays themselves, each with a size of c
for(i=0; i<r; i++)
matrix[i] = new int[c];
Faisal Shahzad Khan 42
Dynamic allocation of 2D arrays
(Cont ..)
 The elements of the array matrix now can be
accessed by the matrix[i][j] notation
 Keep in mind, the entire array is not in contiguous
space (unlike a static 2D array)
 The elements of each row are in contiguous space,
but the rows themselves are not.
 matrix[i][j+1] is after matrix[i][j] in memory,
but matrix[i][0] may be before or after
matrix[i+1][0] in memory
Faisal Shahzad Khan 43
Example
// create a 2D array dynamically
int rows, columns, i, j;
int **matrix;
cin >> rows >> columns;
matrix = new int*[rows];
for(i=0; i<rows; i++)
matrix[i] = new int[columns];
Faisal Shahzad Khan 44
// deallocate the array
for(i=0; i<rows; i++)
delete [] matrix[i];
delete [] matrix;
Faisal Shahzad Khan 45
Passing pointers to a function
Pointers as arguments to
functions
 Pointers can be passed to functions just like other
types.
 Just as with any other argument, verify that the
number and type of arguments in function
invocation match the prototype (and function
header).
Faisal Shahzad Khan 46
Example of pointer arguments
void Swap(int *p1, int *p2);
void main ()
{
int x, y;
cin >> x >> y;
cout << x << " " << y << endl;
Swap(&x,&y); // passes addresses of x and y explicitly
cout << x << " " << y << endl;
}
void Swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
Faisal Shahzad Khan 47
Example of reference arguments
void Swap(int &a, int &b);
void main ()
{
int x, y;
cin >> x >> y;
cout << x << " " << y << endl;
Swap(x,y); // passes addresses of x and y implicitly
cout << x << " " << y << endl;
}
void Swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
Faisal Shahzad Khan 48
More example
Faisal Shahzad Khan 49
void main ()
{
int r, s = 5, t = 6;
int *tp = &t;
r = MyFunction(tp,s);
r = MyFunction(&t,s);
r = MyFunction(&s,*tp);
}
int MyFunction(int *p, int i)
{
*p = 3;
i = 4;
return i;
}
Faisal Shahzad Khan 50
Memory leaks and
Dangling Pointers
Memory leaks
 When you dynamically create objects, you can
access them through the pointer which is assigned
by the new operator
 Reassigning a pointer without deleting the
memory it pointed to previously is called a
memory leak
 It results in loss of available memory space
Faisal Shahzad Khan 51
Memory leak example
Faisal Shahzad Khan 52
int *ptr1 = new int;
int *ptr2 = new int;
*ptr1 = 8;
*ptr2 = 5;
ptr1
8
5
ptr2
ptr1
8
5
ptr2
ptr2 = ptr1;
How to avoid?
Inaccessible object
 An inaccessible object is an unnamed object that was
created by operator new and which a programmer has
left without a pointer to it.
 It is a logical error and causes memory leaks.
Faisal Shahzad Khan 53
Dangling Pointer
 It is a pointer that points to dynamic memory that
has been deallocated.
 The result of dereferencing a dangling pointer is
unpredictable.
Faisal Shahzad Khan 54
Faisal Shahzad Khan 55
Dangling Pointer example
int *ptr1 = new int;
int *ptr2;
*ptr1 = 8;
ptr2 = ptr1;
ptr1
8
ptr2
delete ptr1;
ptr1
ptr2How to avoid?
Pointers to objects
Pointers to objects
 Any type that can be used to declare a
variable/object can also have a pointer type.
 Consider the following class:
Faisal Shahzad Khan 57
class Rational
{
private:
int numerator;
int denominator;
public:
Rational(int n, int d);
void Display();
};
Pointers to objects (Cont..)
Faisal Shahzad Khan 58
Rational *rp = NULL;
Rational r(3,4);
rp = &r;
0FFF0
FFF1
FFF2
FFF3
FFF4
FFF5
FFF6
FFF7
FFF8
FFF9
FFFA
FFFB
FFFC
FFFD
rp
Pointers to objects (Cont..)
Faisal Shahzad Khan 59
Rational *rp = NULL;
Rational r(3,4);
rp = &r;
0FFF0
numerator = 3
denominator = 4
FFF1
FFF2
FFF3
3FFF4
FFF5
FFF6
FFF7
4FFF8
FFF9
FFFA
FFFB
FFFC
FFFD
rp
r
Pointers to objects (Cont..)
Faisal Shahzad Khan 60
Rational *rp = NULL;
Rational r(3,4);
rp = &r;
FFF4FFF0
numerator = 3
denominator = 4
FFF1
FFF2
FFF3
3FFF4
FFF5
FFF6
FFF7
4FFF8
FFF9
FFFA
FFFB
FFFC
FFFD
r
rp
Pointers to objects (Cont..)
 If rp is a pointer to an object, then two notations can
be used to reference the instance/object rp points to.
 Using the de-referencing operator *
(*rp).Display();
 Using the member access operator ->
rp -> Display();
Faisal Shahzad Khan 61
Dynamic Allocation of a Class Object
 Consider the Rational class defined before
Rational *rp;
int a, b;
cin >> a >> b;
rp = new Rational(a,b);
(*rp).Display(); // rp->Display();
delete rp;
rp = NULL;
Faisal Shahzad Khan 62

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Pointers
PointersPointers
Pointers
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Structure and union
Structure and unionStructure and union
Structure and union
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
virtual function
virtual functionvirtual function
virtual function
 
File in C language
File in C languageFile in C language
File in C language
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Strings
StringsStrings
Strings
 

Similar a Pointers in C/C++ Programming

Notes fp201-pointer notes
Notes fp201-pointer notesNotes fp201-pointer notes
Notes fp201-pointer notesSiti Nadirah
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphismramya marichamy
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxhappycocoman
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptgeorgejustymirobi1
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)Ameer Hamxa
 

Similar a Pointers in C/C++ Programming (20)

Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
Notes fp201-pointer notes
Notes fp201-pointer notesNotes fp201-pointer notes
Notes fp201-pointer notes
 
Pointers.ppt
Pointers.pptPointers.ppt
Pointers.ppt
 
Pointers.ppt
Pointers.pptPointers.ppt
Pointers.ppt
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Session 5
Session 5Session 5
Session 5
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Pointers
PointersPointers
Pointers
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 

Más de Faisal Shahzad Khan

ERD for Library management system Database
ERD for Library management system DatabaseERD for Library management system Database
ERD for Library management system DatabaseFaisal Shahzad Khan
 
What is AES? Advanced Encryption Standards
What is AES? Advanced Encryption StandardsWhat is AES? Advanced Encryption Standards
What is AES? Advanced Encryption StandardsFaisal Shahzad Khan
 
Story boarding, Handwritten recognition, Inductive, subductive, abductive met...
Story boarding, Handwritten recognition, Inductive, subductive, abductive met...Story boarding, Handwritten recognition, Inductive, subductive, abductive met...
Story boarding, Handwritten recognition, Inductive, subductive, abductive met...Faisal Shahzad Khan
 
MS Access Database Project proposal on Airline Reservation System
MS Access Database Project proposal on Airline Reservation SystemMS Access Database Project proposal on Airline Reservation System
MS Access Database Project proposal on Airline Reservation SystemFaisal Shahzad Khan
 

Más de Faisal Shahzad Khan (9)

ERD for Library management system Database
ERD for Library management system DatabaseERD for Library management system Database
ERD for Library management system Database
 
What is AES? Advanced Encryption Standards
What is AES? Advanced Encryption StandardsWhat is AES? Advanced Encryption Standards
What is AES? Advanced Encryption Standards
 
Story boarding, Handwritten recognition, Inductive, subductive, abductive met...
Story boarding, Handwritten recognition, Inductive, subductive, abductive met...Story boarding, Handwritten recognition, Inductive, subductive, abductive met...
Story boarding, Handwritten recognition, Inductive, subductive, abductive met...
 
NodeJS Presentation
NodeJS PresentationNodeJS Presentation
NodeJS Presentation
 
MS Access Database Project proposal on Airline Reservation System
MS Access Database Project proposal on Airline Reservation SystemMS Access Database Project proposal on Airline Reservation System
MS Access Database Project proposal on Airline Reservation System
 
Who Am I?
Who Am I?Who Am I?
Who Am I?
 
Non Verbal Communication
Non Verbal CommunicationNon Verbal Communication
Non Verbal Communication
 
Impact of Technology on Job
Impact of Technology on JobImpact of Technology on Job
Impact of Technology on Job
 
Water level indicator alarm
Water level indicator alarmWater level indicator alarm
Water level indicator alarm
 

Último

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Último (20)

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Pointers in C/C++ Programming

  • 1. CSC2110 – Data Structures/Algorithms By Faisal Shahzad Khan BSCS UET Texila 03364180007 Facebook.com/fasii.07
  • 2. Pointers  Pointers  Powerful feature of the C++ language  One of the most difficult to master  Essential for construction of interesting data structures 2Faisal Shahzad Khan
  • 3. Addresses and Pointers  C++ allows two ways of accessing variables  Name (C++ keeps track of the address of the first location allocated to the variable)  Address/Pointer  Symbol & gets the address of the variable that follows it  Addresses/Pointers can be displayed by the cout statement  Addresses displayed in HEXADECIMAL Faisal Shahzad Khan 3
  • 4. Example #include <iostream.h> void main( ) { int data = 100; float value = 56.47; cout << data << &data << endl; cout << value << &value << endl; } Output: 100 FFF4 56.47 FFF0 Faisal Shahzad Khan 4 56.47 100 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 value data
  • 5. Pointer Variables  The pointer data type  A data type for containing an address rather than a data value  Integral, similar to int  Size is the number of bytes in which the target computer stores a memory address  Provides indirect access to values Faisal Shahzad Khan 5
  • 6. Declaration of Pointer Variables  A pointer variable is declared by: dataType *pointerVarName;  The pointer variable pointerVarName is used to point to a value of type dataType  The * before the pointerVarName indicates that this is a pointer variable, not a regular variable  The * is not a part of the pointer variable name Faisal Shahzad Khan 6
  • 7. Declaration of Pointer Variables (Cont ..)  Example int *ptr1; float *ptr2;  ptr1 is a pointer to an int value i.e., it can have the address of the memory location (or the first of more than one memory locations) allocated to an int value  ptr2 is a pointer to a float value i.e., it can have the address of the memory location (or the first of more than one memory locations) allocated to a float value Faisal Shahzad Khan 7
  • 8. Declaration of Pointer Variables (Cont ..)  Whitespace doesn’t matter and each of the following will declare ptr as a pointer (to a float) variable and data as a float variable float *ptr, data; float* ptr, data; float (*ptr), data; float data, *ptr; Faisal Shahzad Khan 8
  • 9. Assignment of Pointer Variables  A pointer variable has to be assigned a valid memory address before it can be used in the program  Example: float data = 50.8; float *ptr; ptr = &data;  This will assign the address of the memory location allocated for the floating point variable data to the pointer variable ptr. This is OK, since the variable data has already been allocated some memory space having a valid address Faisal Shahzad Khan 9
  • 10. Assignment of Pointer Variables (Cont ..) Faisal Shahzad Khan 10 float data = 50.8; float *ptr; ptr = &data; 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 data
  • 11. Assignment of Pointer Variables (Cont ..) Faisal Shahzad Khan 11 float data = 50.8; float *ptr; ptr = &data; 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data
  • 12. Assignment of Pointer Variables (Cont ..) Faisal Shahzad Khan 12 float data = 50.8; float *ptr; ptr = &data; FFF4 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data
  • 13. Assignment of Pointer Variables (Cont ..)  Don’t try to assign a specific integer value to a pointer variable since it can be disastrous float *ptr; ptr = 120; Faisal Shahzad Khan 13 • You cannot assign the address of one type of variable to a pointer variable of another type even though they are both integrals int data = 50; float *ptr; ptr = &data;
  • 14. Initializing pointers  A pointer can be initialized during declaration by assigning it the address of an existing variable float data = 50.8; float *ptr = &data;  If a pointer is not initialized during declaration, it is wise to give it a NULL (0) value int *ip = 0; float *fp = NULL; Faisal Shahzad Khan 14
  • 15. The NULL pointer  The NULL pointer is a valid address for any data type.  But NULL is not memory address 0.  It is an error to dereference a pointer whose value is NULL.  Such an error may cause your program to crash, or behave erratically.  It is the programmer’s job to check for this. Faisal Shahzad Khan 15
  • 16. Dereferencing  Dereferencing – Using a pointer variable to access the value stored at the location pointed by the variable  Provide indirect access to values and also called indirection  Done by using the dereferencing operator * in front of a pointer variable  Unary operator  Highest precedence Faisal Shahzad Khan 16
  • 17. Dereferencing (Cont ..)  Example: float data = 50.8; float *ptr; ptr = &data; cout << *ptr;  Once the pointer variable ptr has been declared, *ptr represents the value pointed to by ptr (or the value located at the address specified by ptr) and may be treated like any other variable of float type Faisal Shahzad Khan 17
  • 18. Dereferencing (Cont ..)  The dereferencing operator * can also be used in assignments. *ptr = 200;  Make sure that ptr has been properly initialized Faisal Shahzad Khan 18
  • 19. Dereferencing Example Faisal Shahzad Khan 19 #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: FFF4 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data
  • 20. Dereferencing Example (Cont ..) Faisal Shahzad Khan 20 #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: FFF4 50.80 FFF4 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data
  • 21. Dereferencing Example (Cont ..) Faisal Shahzad Khan 21 FFF4 27.4 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output:
  • 22. Dereferencing Example (Cont ..) Faisal Shahzad Khan 22 FFF4 27.4 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: 27.4
  • 23. Dereferencing Example (Cont ..) Faisal Shahzad Khan 23 FFF4 27.4 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data #include <iostream.h> void main() { float data = 50.8; float *ptr; ptr = &data; cout << ptr << *ptr << endl; *ptr = 27.4; cout << *ptr << endl; cout << data << endl; } Output: 27.4
  • 24. Operations on Pointer Variables  Assignment – the value of one pointer variable can be assigned to another pointer variable of the same type  Relational operations - two pointer variables of the same type can be compared for equality, and so on  Some limited arithmetic operations  integer values can be added to and subtracted from a pointer variable  value of one pointer variable can be subtracted from another pointer variable Faisal Shahzad Khan 24
  • 25. Pointers to arrays  A pointer variable can be used to access the elements of an array of the same type. int gradeList[8] = {92,85,75,88,79,54,34,96}; int *myGrades = gradeList; cout << gradeList[1]; cout << *myGrades; cout << *(myGrades + 2); cout << myGrades[3];  Note that the array name gradeList acts like the pointer variable myGrades. Faisal Shahzad Khan 25
  • 26. Faisal Shahzad Khan 26 Dynamic Memory Allocation
  • 27. Types of Program Data  Static Data: Memory allocation exists throughout execution of program  Automatic Data: Automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function  Dynamic Data: Explicitly allocated and deallocated during program execution by C++ instructions written by programmer Faisal Shahzad Khan 27
  • 28. Allocation of Memory  Static Allocation: Allocation of memory space at compile time.  Dynamic Allocation: Allocation of memory space at run time. Faisal Shahzad Khan 28
  • 29. Dynamic memory allocation  Dynamic allocation is useful when  arrays need to be created whose extent is not known until run time  complex structures of unknown size and/or shape need to be constructed as the program runs  objects need to be created and the constructor arguments are not known until run time Faisal Shahzad Khan 29
  • 30. Dynamic memory allocation  Pointers need to be used for dynamic allocation of memory  Use the operator new to dynamically allocate space  Use the operator delete to later free this space Faisal Shahzad Khan 30
  • 31. The new operator  If memory is available, the new operator allocates memory space for the requested object/array, and returns a pointer to (address of) the memory allocated.  If sufficient memory is not available, the new operator returns NULL.  The dynamically allocated object/array exists until the delete operator destroys it. Faisal Shahzad Khan 31
  • 32. The delete operator  The delete operator deallocates the object or array currently pointed to by the pointer which was previously allocated at run-time by the new operator.  the freed memory space is returned to Heap  the pointer is then considered unassigned  If the value of the pointer is NULL there is no effect. Faisal Shahzad Khan 32
  • 33. Example Faisal Shahzad Khan 33 FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptrint *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL;
  • 34. Faisal Shahzad Khan 34 Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; 0EC4 FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptr
  • 35. Faisal Shahzad Khan 35 Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; 0EC4 22 FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptr
  • 36. Faisal Shahzad Khan 36 Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; 0EC4 22 FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptr Output: 22
  • 37. Faisal Shahzad Khan 37 Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; ? FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptr
  • 38. Faisal Shahzad Khan 38 Example (Cont ..) int *ptr; ptr = new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL; 0 FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptr
  • 39. Dynamic allocation and deallocation of arrays  Use the [IntExp] on the new statement to create an array of objects instead of a single instance.  On the delete statement use [] to indicate that an array of objects is to be deallocated. Faisal Shahzad Khan 39
  • 40. Example of dynamic array allocation Faisal Shahzad Khan 40 int* grades = NULL; int numberOfGrades; cout << "Enter the number of grades: "; cin >> numberOfGrades; grades = new int[numberOfGrades]; for (int i = 0; i < numberOfGrades; i++) cin >> grades[i]; for (int j = 0; j < numberOfGrades; j++) cout << grades[j] << " "; delete [] grades; grades = NULL;
  • 41. Dynamic allocation of 2D arrays  A two dimensional array is really an array of arrays (rows).  To dynamically declare a two dimensional array of int type, you need to declare a pointer to a pointer as: int **matrix; Faisal Shahzad Khan 41
  • 42. Dynamic allocation of 2D arrays (Cont ..)  To allocate space for the 2D array with r rows and c columns:  You first allocate the array of pointers which will point to the arrays (rows) matrix = new int*[r];  This creates space for r addresses; each being a pointer to an int.  Then you need to allocate the space for the 1D arrays themselves, each with a size of c for(i=0; i<r; i++) matrix[i] = new int[c]; Faisal Shahzad Khan 42
  • 43. Dynamic allocation of 2D arrays (Cont ..)  The elements of the array matrix now can be accessed by the matrix[i][j] notation  Keep in mind, the entire array is not in contiguous space (unlike a static 2D array)  The elements of each row are in contiguous space, but the rows themselves are not.  matrix[i][j+1] is after matrix[i][j] in memory, but matrix[i][0] may be before or after matrix[i+1][0] in memory Faisal Shahzad Khan 43
  • 44. Example // create a 2D array dynamically int rows, columns, i, j; int **matrix; cin >> rows >> columns; matrix = new int*[rows]; for(i=0; i<rows; i++) matrix[i] = new int[columns]; Faisal Shahzad Khan 44 // deallocate the array for(i=0; i<rows; i++) delete [] matrix[i]; delete [] matrix;
  • 45. Faisal Shahzad Khan 45 Passing pointers to a function
  • 46. Pointers as arguments to functions  Pointers can be passed to functions just like other types.  Just as with any other argument, verify that the number and type of arguments in function invocation match the prototype (and function header). Faisal Shahzad Khan 46
  • 47. Example of pointer arguments void Swap(int *p1, int *p2); void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(&x,&y); // passes addresses of x and y explicitly cout << x << " " << y << endl; } void Swap(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } Faisal Shahzad Khan 47
  • 48. Example of reference arguments void Swap(int &a, int &b); void main () { int x, y; cin >> x >> y; cout << x << " " << y << endl; Swap(x,y); // passes addresses of x and y implicitly cout << x << " " << y << endl; } void Swap(int &a, int &b) { int temp = a; a = b; b = temp; } Faisal Shahzad Khan 48
  • 49. More example Faisal Shahzad Khan 49 void main () { int r, s = 5, t = 6; int *tp = &t; r = MyFunction(tp,s); r = MyFunction(&t,s); r = MyFunction(&s,*tp); } int MyFunction(int *p, int i) { *p = 3; i = 4; return i; }
  • 50. Faisal Shahzad Khan 50 Memory leaks and Dangling Pointers
  • 51. Memory leaks  When you dynamically create objects, you can access them through the pointer which is assigned by the new operator  Reassigning a pointer without deleting the memory it pointed to previously is called a memory leak  It results in loss of available memory space Faisal Shahzad Khan 51
  • 52. Memory leak example Faisal Shahzad Khan 52 int *ptr1 = new int; int *ptr2 = new int; *ptr1 = 8; *ptr2 = 5; ptr1 8 5 ptr2 ptr1 8 5 ptr2 ptr2 = ptr1; How to avoid?
  • 53. Inaccessible object  An inaccessible object is an unnamed object that was created by operator new and which a programmer has left without a pointer to it.  It is a logical error and causes memory leaks. Faisal Shahzad Khan 53
  • 54. Dangling Pointer  It is a pointer that points to dynamic memory that has been deallocated.  The result of dereferencing a dangling pointer is unpredictable. Faisal Shahzad Khan 54
  • 55. Faisal Shahzad Khan 55 Dangling Pointer example int *ptr1 = new int; int *ptr2; *ptr1 = 8; ptr2 = ptr1; ptr1 8 ptr2 delete ptr1; ptr1 ptr2How to avoid?
  • 57. Pointers to objects  Any type that can be used to declare a variable/object can also have a pointer type.  Consider the following class: Faisal Shahzad Khan 57 class Rational { private: int numerator; int denominator; public: Rational(int n, int d); void Display(); };
  • 58. Pointers to objects (Cont..) Faisal Shahzad Khan 58 Rational *rp = NULL; Rational r(3,4); rp = &r; 0FFF0 FFF1 FFF2 FFF3 FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD rp
  • 59. Pointers to objects (Cont..) Faisal Shahzad Khan 59 Rational *rp = NULL; Rational r(3,4); rp = &r; 0FFF0 numerator = 3 denominator = 4 FFF1 FFF2 FFF3 3FFF4 FFF5 FFF6 FFF7 4FFF8 FFF9 FFFA FFFB FFFC FFFD rp r
  • 60. Pointers to objects (Cont..) Faisal Shahzad Khan 60 Rational *rp = NULL; Rational r(3,4); rp = &r; FFF4FFF0 numerator = 3 denominator = 4 FFF1 FFF2 FFF3 3FFF4 FFF5 FFF6 FFF7 4FFF8 FFF9 FFFA FFFB FFFC FFFD r rp
  • 61. Pointers to objects (Cont..)  If rp is a pointer to an object, then two notations can be used to reference the instance/object rp points to.  Using the de-referencing operator * (*rp).Display();  Using the member access operator -> rp -> Display(); Faisal Shahzad Khan 61
  • 62. Dynamic Allocation of a Class Object  Consider the Rational class defined before Rational *rp; int a, b; cin >> a >> b; rp = new Rational(a,b); (*rp).Display(); // rp->Display(); delete rp; rp = NULL; Faisal Shahzad Khan 62