1 / 22

Pointers

Pointers. Dynamic Memory. 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];

cybill
Télécharger la présentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 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 Static vs. Dynamic Objects

  6. Dynamic Memory C++ “new” keyword new <type>; // allocate size of the type new<type>[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 <int>, you need an <int> pointer. If you request <float>, you need a <float> pointer.

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

  9. new p new p new p 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];

  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 storeaddresses.

  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 <address, i.e. pointer variable>; // a variable • example: • int *p= new int; • delete p; • delete [] <address, i.e. pointer variable>; // 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; deletep; // q is a dangling pointer p int q p int delete p q

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

  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 withthe following program? • int main( ){ • int *p, *r; • p = new int; • r = new int; • p = 1; • r = p; • return 0; • }

More Related