1 / 12

CSE 143 Section AD

CSE 143 Section AD. Quiz Section 8. Announcements. If you are not yet officially registered for this course, please make sure to talk to me after class. There will be another debugger tutorial today at 3:30 in MGH Room 30.

Télécharger la présentation

CSE 143 Section AD

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. CSE 143 Section AD Quiz Section 8 Jeff West - Quiz Section 8

  2. Announcements • If you are not yet officially registered for this course, please make sure to talk to me after class. • There will be another debugger tutorial today at 3:30 in MGH Room 30. • I’d like to welcome as much feedback as possible about how well quiz sections are preparing you so please feel free to talk to me about them… I have linked a feedback form from the Section AD web page if you want to remain anonymous… • Grades are on the course website – check to make sure yours are right ;) • There is a midterm in one week… if there is anything at all that is unclear to you please email me or visit my office hours this week so that studying for the midterm next week can be as painless as possible. Jeff West - Quiz Section 8

  3. Pointer Types (1) What will the following code print to the screen? int i = 3; int j = 3; int* ip1 = &i; int* ip2 = &j; if(ip1 == ip2) cout << “They are equal”; else cout << “They are unequal”; Jeff West - Quiz Section 8

  4. Pointer Types (2) That printed “They are unequal”… What will the following code print to the screen? int i = 3; int j = 3; int* ip1 = &i; int* ip2 = &j; if(*ip1 == *ip2) cout << “They are equal”; else cout << “They are unequal”; Jeff West - Quiz Section 8

  5. Pointer Types (3) That printed “They are equal”… What will the following code print to the screen? int i = 3; int* ip1 = &i; int* ip2 = ip1; if(&ip1 == &ip2) cout << “They are equal”; else cout << “They are unequal”; Jeff West - Quiz Section 8

  6. new That printed “They are unequal”… int i = 3; int* ip = &i; What if you don’t want to take up space for i until you are actually going to use it? int* ip; ip = new int; // allocate new integer *ip = 3; // assign 3 to int you created Jeff West - Quiz Section 8

  7. delete When you’re done using an object you can free up the memory it uses by deleting it! Example: int* ip; ip = new int; // allocate new integer *ip = 3; // assign 3 to int you created delete ip; // deallocates integer // what about ip?? Jeff West - Quiz Section 8

  8. newing and deleting arrays int *p; p = new int[10]; for(int i = 0; i < 10; i++) p[i] = 2 * i; cout << p[2]; delete [] p; // USE THE RIGHT // VERSION OF // DELETE Jeff West - Quiz Section 8

  9. Memory Leak (Losing the Pointer) A memory leak occurs when you lose the pointer to memory without giving the memory back to the operating system. The memory will just sit in the heap with a “used” flag on it but you will have no way to access it! Example: int* ip; P = new int; *p = 12; p = new int; // reassigned p… what about // where it used to point? *p = 17; Jeff West - Quiz Section 8

  10. Dangling Pointers (Losing the Heap Space) A dangling pointer is sort of the “opposite problem.” A dangling pointer is a pointer that “thinks” that it is pointing to an object in memory when in reality the object in memory has already been returned to the operating system. Example: int *ip1 = new int; int *ip2; *p = 12; q = p; delete p; *q = 15; // where will 15 be stored? Jeff West - Quiz Section 8

  11. Okay neat, now what? Okay, so you can get new values… but how can this possibly help us in the “real” world? Imagine creating an internet game (EverQuest, Quake, whatever you like)… you want to allow as many as 2,000 characters to exist in your game at one time but you also want to be as efficient as possible… if your game stores all characters active in the game in an array of character objects, how big should the array be? Jeff West - Quiz Section 8

  12. Answer Answer: There should only be space allocated for the number of characters that are actually active (or somewhere in that ballpark). But how can you possibly make it only as big as it needs to be when there will be a different number of characters in the game at different times? Answer: Dynamic Memory! We’ll get really in depth on ways to grow and shrink array sizes tomorrow, so stay tuned… Jeff West - Quiz Section 8

More Related