Instructor : Muhammad Waqar Khan
 Basic Introductions About Pointers
 Dynamic Memory Allocation
 Memory Leaks and Dangling pointers
Data Structures
 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
Data Structures
#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
Data Structures
56.47
100
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
value
data
 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
Data Structures
Declaration of Pointer VariablesDeclaration of Pointer Variables
‱ A pointer variable is declared by:
dataType *pointerVarName;
o The pointer variable pointerVarName is used to point to a value of
type dataType
o The * before the pointerVarName indicates that this is a pointer
variable, not a regular variable
o The * is not a part of the pointer variable name
5
Data Structures
float data = 50.8;
float *ptr;
ptr = &data; 50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
data
Data Structures
float data = 50.8;
float *ptr;
ptr = &data; 50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
Data Structures
float data = 50.8;
float *ptr;
ptr = &data;
FFF4
50.8
FFF1
FFF0
FFF2
FFF3
FFF4
FFF5
FFF6
ptr
data
 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;
Data Structures
Data Structures
Memory Allocation
‱ Static Allocation: Allocation of memory
space at compile time.
‱ Static Allocation means, that the memory for your variables is automatically allocated, You do
not have to reserve extra memory using them, but on the other hand, have also no control
over the lifetime of this memory. E.g: a variable in a function, is only there until the
function finishes.
‱ void func() { int i; /* `i` only exists during `func` */ }
‱ Dynamic Allocation: Allocation of memory
space at run time.
‱ Dynamic memory allocation is a bit different. You now control the exact size and the lifetime
of these memory locations. If you don't free it, you'll run into memory leaks, which may cause
your application to crash, since it, at some point cannot allocation more memory.
‱ int* func() { int* mem = malloc(1024); return mem; } int* mem = func(); /* still accessible */
Data Structures
 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
Data Structures
Data Structures
FDE1
FDE0
0EC7
FDE2
FDE3
0EC4
0EC5
0EC6
ptrint *ptr;
ptr = new int;
*ptr = 22;
cout << *ptr << endl;
delete ptr;
ptr = NULL;
Data Structures
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
Data Structures
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
Data Structures
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
Data Structures
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
Data Structures
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
Data Structures
Memory leaks and
Dangling Pointers
 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
Data Structures
Data Structures
Dangling Pointer example
int *ptr1 = new int;
int *ptr2;
*ptr1 = 8;
ptr2 = ptr1;
ptr1
8
ptr2
delete ptr1;
ptr1
ptr2
Data Structures
omair.ansari92@gmail.com
omistechmaster.blogspot.com

Pointers - DataStructures

  • 1.
  • 2.
     Basic IntroductionsAbout Pointers  Dynamic Memory Allocation  Memory Leaks and Dangling pointers Data Structures
  • 3.
     C++ allowstwo 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 Data Structures
  • 4.
    #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 Data Structures 56.47 100 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 value data
  • 5.
     The pointerdata 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 Data Structures Declaration of Pointer VariablesDeclaration of Pointer Variables ‱ A pointer variable is declared by: dataType *pointerVarName; o The pointer variable pointerVarName is used to point to a value of type dataType o The * before the pointerVarName indicates that this is a pointer variable, not a regular variable o The * is not a part of the pointer variable name 5
  • 6.
    Data Structures float data= 50.8; float *ptr; ptr = &data; 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 data
  • 7.
    Data Structures float data= 50.8; float *ptr; ptr = &data; 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data
  • 8.
    Data Structures float data= 50.8; float *ptr; ptr = &data; FFF4 50.8 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 ptr data
  • 9.
     A pointercan 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; Data Structures
  • 10.
  • 11.
    ‱ Static Allocation:Allocation of memory space at compile time. ‱ Static Allocation means, that the memory for your variables is automatically allocated, You do not have to reserve extra memory using them, but on the other hand, have also no control over the lifetime of this memory. E.g: a variable in a function, is only there until the function finishes. ‱ void func() { int i; /* `i` only exists during `func` */ } ‱ Dynamic Allocation: Allocation of memory space at run time. ‱ Dynamic memory allocation is a bit different. You now control the exact size and the lifetime of these memory locations. If you don't free it, you'll run into memory leaks, which may cause your application to crash, since it, at some point cannot allocation more memory. ‱ int* func() { int* mem = malloc(1024); return mem; } int* mem = func(); /* still accessible */ Data Structures
  • 12.
     Pointers needto be used for dynamic allocation of memory  Use the operator new to dynamically allocate space  Use the operator delete to later free this space Data Structures
  • 13.
    Data Structures FDE1 FDE0 0EC7 FDE2 FDE3 0EC4 0EC5 0EC6 ptrint *ptr; ptr= new int; *ptr = 22; cout << *ptr << endl; delete ptr; ptr = NULL;
  • 14.
    Data Structures 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
  • 15.
    Data Structures 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
  • 16.
    Data Structures 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
  • 17.
    Data Structures 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
  • 18.
    Data Structures 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
  • 19.
    Data Structures Memory leaksand Dangling Pointers
  • 20.
     When youdynamically 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 Data Structures
  • 21.
    Data Structures Dangling Pointerexample int *ptr1 = new int; int *ptr2; *ptr1 = 8; ptr2 = ptr1; ptr1 8 ptr2 delete ptr1; ptr1 ptr2
  • 22.