Pointers and Dynamic Memory - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Pointers and Dynamic Memory

Description:

Dangling Pointer (1) //wrong version that causes the dangling pointer problem. int *P; ... Dangling Pointer (2) //correct version. int *P; P = new int(10) ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 8
Provided by: yxie
Category:

less

Transcript and Presenter's Notes

Title: Pointers and Dynamic Memory


1
Pointers and Dynamic Memory
  • Dr. Ying Xie

2
  • Dynamically Allocate Memories
  • Memory allocation operator new
  • int p1 new int
  • int p2 new int(2)
  • int p3 new int10
  • Memory deallocation operator delete
  • delete p1
  • delete p2
  • delete p3

3
  • Memory Leaking
  • void myfunction()
  •     int pt    int ordinaryVariable    pt
    new int(1024)    ....    ....    //No
    delete
  • int main()
  •     while (some condition exists) //
    Pseudo-code            myfunction()        
    exit 0

4
  • Memory Leaking (1)
  • //wrong version which cause memory leaking
  • void myfunction()
  •     int pt    int ordinaryVariable    pt
    new int(1024)    ....    ....    //No
    delete
  • int main()
  •     while (some condition exists) //
    Pseudo-code            myfunction()        
    exit 0

5
  • Memory Leaking (2)
  • //correct version
  • void myfunction()
  •     int pt    int ordinaryVariable    pt
    new int(1024)    ....    ....    delete pt
  • int main()
  •     while (some condition exists) //
    Pseudo-code            myfunction()        
    exit 0

6
  • Dangling Pointer (1)
  • //wrong version that causes the dangling pointer
    problem
  • int PP new int(10)delete P
  • P 5

7
  • Dangling Pointer (2)
  • //correct version
  • int PP new int(10)delete PP 0
Write a Comment
User Comments (0)
About PowerShow.com