{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2-9 - Problem Solving with Computers-II CS 24 February 9...

Info icon This preview shows pages 1–19. Sign up to view the full content.

View Full Document Right Arrow Icon
Problem Solving with Computers-II CS 24 February 9, 2010 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Announcements Wednesday’s Lab section Programming project Midterm statistics Average and median: 35-36 Maximum: 45 2
Image of page 2
Recap Binary search Exception handling 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture Plan Memory allocation Exception handling File I/O Stack ADT 4
Image of page 4
Big-O of Sorted List Operations (Array-based) Constructor O(1) IsFull O(1) GetLength O(1) RetrieveItem O(log N) MakeEmpty O(1) InsertItem O(N) DeleteItem O(N) ResetList O(1) GetNextItem O(1) 5 How about pointer-based implementation?
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Allocation of memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time . DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new .
Image of page 6
7 Three Kinds of Program Data STATIC DATA : memory allocation exists throughout execution of program. static long SeedValue; AUTOMATIC DATA : automatically created at function entry, resides in activation frame of the function , and is destroyed when returning from function. DYNAMIC DATA : explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, throws a bad_alloc exception. The dynamically allocated object exists until the delete operator destroys it. 8
Image of page 8
char* ptr; ptr = new char; *ptr = ‘B’; std::cout << *ptr; 9 2000 ptr Dynamically Allocated Data
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; std::cout << *ptr; NOTE: Dynamic data has no variable name 2000 ptr
Image of page 10
11 Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; std::cout << *ptr; NOTE: Dynamic data has no variable name 2000 ptr ‘B’
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
12 Dynamically Allocated Data char* ptr; ptr = new char; *ptr = ‘B’; std::cout << *ptr; delete ptr; 2000 ptr NOTE: Delete deallocates the memory pointed to by ptr. ?
Image of page 12
The object or array currently pointed to by the pointer is deallocated , and the pointer is considered unassigned. The memory is returned to the free store. The Delete Operator 13
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
14 Dynamic Array Allocation char *ptr; // ptr is a pointer variable that // can hold the address of a char ptr = new char[ 5 ]; // dynamically, during run time, allocates // memory for 5 characters and places into // the contents of ptr their beginning address ptr 6000 6000
Image of page 14
15 char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; // a pointer can be subscripted std::cout << ptr[ 2] ; ptr 6000 6000 ‘B’ ‘y’ ‘e’ ‘\0’ ‘u’
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
16 Dynamic Array Deallocation char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; delete ptr; // deallocates array pointed to by ptr // ptr itself is not deallocated, but // the value of ptr is considered unassigned ptr ?
Image of page 16
17 int* ptr = new int; *ptr = 3; ptr = new int; // changes value of ptr *ptr = 4; What Happens Here?
Image of page 17

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
18 Memory Leak A memory leak occurs when dynamic memory (that was created using operator new ) has been left without a pointer to it by the programmer, and so is inaccessible.
Image of page 18
Image of page 19
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern