Presentation is loading. Please wait.

Presentation is loading. Please wait.

This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.

Similar presentations


Presentation on theme: "This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson."— Presentation transcript:

1 This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson Learning, 2000. Pointers Dynamic Memory

2 MEMORY MANAGEMENT STATIC MEMORY Until now, we have only learned about static memory Memory is allocated at compilation time Declaration of large memory variables (arrays) must be determined before the program is compiled. int students[10]; what if we don’t know the exact number of students in advance? DYANMIC MEMORY We can “ask” for memory while the program is running. We can allocate and release memory ourselves! More powerful programming ability (also more potential for errors!)

3 Memory Allocation { int a[200]; int *ptr = a; … } int *ptr = new int[200]; … delete [] ptr;

4 Conceptual View of Memory (DYNAMIC MEMORY)

5 Static vs. Dynamic Objects Static object Memory is acquired automatically Memory is returned automatically when object goes out of scope Dynamic object Memory is acquired by program with an allocation request new operation Dynamic objects can exist beyond the function in which they were allocated Object memory is returned by a deallocation request delete operation

6 Dynamic Memory C++ “new” keyword new ;// allocate size of the type new [size];// allocate an array Asks the Operating System to return you a pointer to a “chunk” of data.

7 Allocating Memory Operating System Manages dynamic memory. You have: int *p; p = new int[1000]; Ask OS to find a segment Of memory of 1000*4 btyes OS returns the address. So, you need a pointer to the type you requested. If you request, you need an pointer. If you request, you need a pointer.

8 Allocating memory using “ new ” 8002 8006 8010 8014 8018 8022 8026 new returns the address to the “start” of the memory it allocated int *p; p = new int[7]; //p is assigned 8002 p[0] = *(p) = *(p+0) = ? p[1] = *(p+1) = ? p[2] = *(p+2) = ? p[0] p[1] p[2]

9 Dynamic Memory Allocation Request for “unused” memory from the Operating System int *p, n=10; p = new int; p = new int[100]; p = new int[n]; p new p p

10 Dynamic Memory Allocation Example Need an array of unknown size int main() { cout << “How many students? “; cin >> n; int *grades = new int[n]; for( int i = 0; i < n; i++) { cout << “Input Grade for Student” << (i+1) << “ ? :”; cin >> grades[i] ; }... // call a function with dynamic array printMean( grades, n );... }

11 Dynamic Memory C++ “delete” keyword delete address; // delete one element at address delete [] address; // delete array at address How can we specify what the address is? We have pointer. Pointer variables store addresses.

12 Freeing (or deleting) Memory The memory you request by “new” can not be used again until you explicitly release it via “delete”. We call this “freeing” or “releasing” or “deleting” memory. int *p; p = new int [1000]; delete [] p; // delete (or free) the array at p NOTE, the value of p will not change! However, it is now no longer points to a valid memory address.

13 Freeing (or deleting) Memory

14 Freeing (or Deleting) memory “ delete ” is a keyword delete ; // a variable example: int *p= new int; delete p; delete [] ; // an array example: int *p = new int[1000]; delete [] p;

15 Dynamic Memory Dynamic Memory is very powerful C++ trusts you as the programmer to manage you own memory While this gives you lots of control, it is easy to make mistakes. Dynamic memory bugs is common in large software projects written in C++.

16 The Dangling Pointer problem Be careful when you delete memory pointed to by p, you are not erasing a location that some other pointer q is pointing to. int *p, *q; P = new int; q = p; delete p; // q is a dangling pointer p q int p q delete p

17 Memory Leak Problem Make sure to delete memory when finished int *p; p = new int[100]; int a; NO ONE HAS ACCESS TO THIS MEMORY 100 integers p = &a; a a 100 integers a a This memory is un-addressable And it is reserved until your program terminates. It is garbage (it has leaked from the system)

18 Exercise 1 What is wrong with the following program?. int main( ){ int x =20; int* p; p = new int; *p = 30; cout << *p << " " << x << endl; delete p; p = &x; delete p; cout << *p << " " << x << endl; return 0; }

19 Exercise 2 What is wrong in the following code? int main( ){ const int SIZE = 10000000; int i; int A[SIZE]; for(i=0; i<SIZE; i++) A[i] = i; int *B; B = new int[SIZE]; for(i=0; i<SIZE; i++) B[i] = A[i]; for(i=0; i<SIZE; i++) cout << A[i] << " " << B[i] << endl; return 0; }

20 Exercise 3 What is the output of the following program? int main( ){ int i, *p, *r, t[4]={0,1,2,3}; p = new int[4]; for(i=0; i<4; i++) p[i] = 6*(i+1); r = t; for(i=3; i>=0; i--) r[i] -= 1; r[2] = 8; for(i=0; i<4; i++) t[i] = p[i] + r[i]; for(i=0; i<4; i++) cout << t[i] << " "; delete [] p; return 0; }

21 Exercise 4 What is the output of the following program? void F1(int* temp){ *temp = 99; } void main( ){ int *p1, *p2; p1 = new int; *p1 = 50; p2 = p1; F1(p2); cout << *p1 << " " << *p2 << endl; p1 = new int; *p1 = 88; cout << *p1 << " " << *p2 << endl; delete p1; delete p2; }

22 Exercise 5 What is wrong with the following program? int main( ){ int *p, *r; p = new int; r = new int; p = 1; r = p; return 0; }


Download ppt "This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson."

Similar presentations


Ads by Google