Upload
omair-imtiaz
View
127
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Bases on important and common concept about pointers in datastructures
Citation preview
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 FFF456.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 Declaration of Pointer VariablesVariables
• A pointer variable is declared by: dataType *pointerVarName;o The pointer variable pointerVarName is used to point to a value of
type dataTypeo The * before the pointerVarName indicates that this is a pointer
variable, not a regular variableo 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
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 andDangling 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
[email protected] omistechmaster.blogspot.com