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

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
Problem Solving with Computers-II CS 24 February 4, 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 Coding up binary search Exception handling Timing binary vs. linear search Programming project Start working on it! Next week’s lab based on it. 2
Background image of page 2
Recap Sorted lists Binary search Time complexity What about pointer-based implementation? 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 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 3 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, the null pointer 0 is returned. The dynamically allocated object exists until the delete operator destroys it. 8 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 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 ;
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

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

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online