22
Instructor : Muhammad Waqar Khan

Pointers - DataStructures

Embed Size (px)

DESCRIPTION

Bases on important and common concept about pointers in datastructures

Citation preview

Page 1: Pointers - DataStructures

Instructor : Muhammad Waqar Khan

Page 2: Pointers - DataStructures

Basic Introductions About Pointers Dynamic Memory Allocation Memory Leaks and Dangling

pointers

Data Structures

Page 3: Pointers - DataStructures

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

Page 4: Pointers - DataStructures

#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

Page 5: Pointers - DataStructures

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

Page 6: Pointers - DataStructures

Data Structures

float data = 50.8;

float *ptr;

ptr = &data; 50.8

FFF1

FFF0

FFF2

FFF3

FFF4

FFF5

FFF6

data

Page 7: Pointers - DataStructures

Data Structures

float data = 50.8;

float *ptr;

ptr = &data; 50.8

FFF1

FFF0

FFF2

FFF3

FFF4

FFF5

FFF6

ptr

data

Page 8: Pointers - DataStructures

Data Structures

float data = 50.8;

float *ptr;

ptr = &data;

FFF4

50.8

FFF1

FFF0

FFF2

FFF3

FFF4

FFF5

FFF6

ptr

data

Page 9: Pointers - DataStructures

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

Page 10: Pointers - DataStructures

Data Structures

Memory Allocation

Page 11: Pointers - DataStructures

• 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

Page 12: Pointers - DataStructures

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

Page 13: Pointers - DataStructures

Data Structures

FDE1

FDE0

0EC7

FDE2

FDE3

0EC4

0EC5

0EC6

ptrint *ptr;

ptr = new int;

*ptr = 22;

cout << *ptr << endl;

delete ptr;

ptr = NULL;

Page 14: Pointers - DataStructures

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

Page 15: Pointers - DataStructures

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

Page 16: Pointers - DataStructures

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

Page 17: Pointers - DataStructures

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

Page 18: Pointers - DataStructures

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

Page 19: Pointers - DataStructures

Data Structures

Memory leaks andDangling Pointers

Page 20: Pointers - DataStructures

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

Page 21: Pointers - DataStructures

Data Structures

Dangling Pointer example

int *ptr1 = new int;

int *ptr2;

*ptr1 = 8;

ptr2 = ptr1;

ptr1

8

ptr2

delete ptr1;

ptr1

ptr2

Page 22: Pointers - DataStructures

Data Structures

[email protected] omistechmaster.blogspot.com