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

Info iconThis 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
Background image of page 1

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

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

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

View Full DocumentRight Arrow Icon
Lecture Plan Memory allocation Exception handling File I/O Stack ADT 4
Background 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?
Background image of page 5

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

View Full DocumentRight 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 .
Background 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
Background image of page 7

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

View Full DocumentRight 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
Background image of page 8
char* ptr; ptr = new char; *ptr = ‘B’; std::cout << *ptr; 9 2000 ptr Dynamically Allocated Data
Background image of page 9

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

View Full DocumentRight 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
Background 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’
Background image of page 11

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

View Full DocumentRight 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. ?
Background 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
Background image of page 13

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

View Full DocumentRight 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
Background 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’
Background image of page 15

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

View Full DocumentRight 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 ?
Background image of page 16
17 int* ptr = new int; *ptr = 3; ptr = new int; // changes value of ptr *ptr = 4; What Happens Here?
Background image of page 17

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

View Full DocumentRight Arrow Icon