SlideShare ist ein Scribd-Unternehmen logo
1 von 22
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

Weitere ähnliche Inhalte

Was ist angesagt?

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and UnionSelvaraj Seerangan
 
Evaluation of Expression in Query Processing
Evaluation of Expression in Query ProcessingEvaluation of Expression in Query Processing
Evaluation of Expression in Query ProcessingNeel Shah
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programAAKASH KUMAR
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptxAnusha sivakumar
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++vivekkumar2938
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries LectureFelipe Costa
 

Was ist angesagt? (20)

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Evaluation of Expression in Query Processing
Evaluation of Expression in Query ProcessingEvaluation of Expression in Query Processing
Evaluation of Expression in Query Processing
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Array in c++
Array in c++Array in c++
Array in c++
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
class and objects
class and objectsclass and objects
class and objects
 
Linked list
Linked listLinked list
Linked list
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 

Andere mochten auch (19)

CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C pointer
C pointerC pointer
C pointer
 
C Pointers
C PointersC Pointers
C Pointers
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Ponters
PontersPonters
Ponters
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
Pointers
PointersPointers
Pointers
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Procedural programming
Procedural programmingProcedural programming
Procedural programming
 
Procedural vs. object oriented programming
Procedural vs. object oriented programmingProcedural vs. object oriented programming
Procedural vs. object oriented programming
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Ähnlich wie Pointers - DataStructures

Notes fp201-pointer notes
Notes fp201-pointer notesNotes fp201-pointer notes
Notes fp201-pointer notesSiti Nadirah
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Programming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedProgramming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedhozaifafadl
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxssuser688516
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++Yung-Yu Chen
 

Ähnlich wie Pointers - DataStructures (20)

Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Pointers.ppt
Pointers.pptPointers.ppt
Pointers.ppt
 
Pointers.ppt
Pointers.pptPointers.ppt
Pointers.ppt
 
Notes fp201-pointer notes
Notes fp201-pointer notesNotes fp201-pointer notes
Notes fp201-pointer notes
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Pointer
PointerPointer
Pointer
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
C pointers
C pointersC pointers
C pointers
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Programming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedProgramming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explained
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Dynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptxDynamic Memory Allocation.pptx
Dynamic Memory Allocation.pptx
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++
 

Mehr von Omair Imtiaz Ansari

Mehr von Omair Imtiaz Ansari (7)

Matlab GUI
Matlab GUIMatlab GUI
Matlab GUI
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Circular wait - Operating Systems
Circular wait - Operating SystemsCircular wait - Operating Systems
Circular wait - Operating Systems
 
Sorting algos
Sorting algosSorting algos
Sorting algos
 
Processor simulator - Computer Archetecture
Processor simulator - Computer ArchetectureProcessor simulator - Computer Archetecture
Processor simulator - Computer Archetecture
 
Communication Skills
Communication SkillsCommunication Skills
Communication Skills
 
Computer Archetecture & Organization
Computer Archetecture & OrganizationComputer Archetecture & Organization
Computer Archetecture & Organization
 

Kürzlich hochgeladen

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
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
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Kürzlich hochgeladen (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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"
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
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
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

Pointers - DataStructures

  • 2.  Basic Introductions About Pointers  Dynamic Memory Allocation  Memory Leaks and Dangling pointers Data Structures
  • 3.  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
  • 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 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
  • 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 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
  • 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 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
  • 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 leaks and Dangling Pointers
  • 20.  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
  • 21. Data Structures Dangling Pointer example int *ptr1 = new int; int *ptr2; *ptr1 = 8; ptr2 = ptr1; ptr1 8 ptr2 delete ptr1; ptr1 ptr2