1 C++ Pointers Dynamic Memory Allocation Pointers within Classes Copy Constructors Destructors STL...
-
Upload
liliana-chandler -
Category
Documents
-
view
237 -
download
3
Transcript of 1 C++ Pointers Dynamic Memory Allocation Pointers within Classes Copy Constructors Destructors STL...
1
C++ Pointers Dynamic Memory Allocation Pointers within Classes
Copy Constructors Destructors
STL list class Iterators Ncurses I/O
The text-based GUI
CSE 30331CSE 30331Lecture 8 – Pointers, Lists & NcursesLecture 8 – Pointers, Lists & Ncurses
2
Pointer Illustration
// x is an int and// ip points to an intint x, *ip; x = 37; ip = &x; the data
// fp points to a// dynamically allocated and // nameless float float *fp; the pointer fp = new float;*fp = -87.5;
A d d res s
D at a co n t en t s
3
Data Addresses in Memory
50
in t eger ch ar
5 0 0 0
S
5 0 0 4 5 0 0 5
4
Pointer declaration format
<type> * <ptr_name>;
The declared pointer is a variable whose value is the address of a data item of the designated type.
int *intPtr;
char *charPtr;
Declaring Pointer Variables
5
int m = 50, *intPtr;
// here intPtr “points to” nothing
intPtr = &m; // &m is the address of m
// now intPtr “point to” m
Assigning Values to Pointers
5 0m
?in tP tr
?
5 0
m
& m
in tP tr(a) A fte r d e c laratio n (b ) A fte r as s ig n m e n t
6
Arrays and Pointers
a rr
a rr[0 ] a rr[6 ]a rr[5 ]a rr[4 ]a rr[3 ]a rr[2 ]a rr[1 ]
a rr+ 7a rr+ 2a rr+ 1
6000 602860206016601260086004 6032
a rr+ 5
The amount the pointer is incremented is based on the sizeof() the base type of the array
Ex: arr+5 is really &arr[0] + 5*sizeof(element_type)
7
Dynamic Memory
Pointer Variable whose value is the address of another data value
Heap System managed store of memory available to be allocated
and deallocated while program is running New
operator to dynamically allocate memory from the heap for program use
Delete operator to dynamically deallocate memory no longer
needed by program and return it to the heap
8
“new” and “delete” operators Allocating & deallocating a single data location
int *p = new int; // allocates itdelete p; // deallocates it
Allocating & deallocating a dynamic array Note square brackets used with delete operator. These ensure
system deallocates ALL locations in array, not just the first one.
// allocate space for 300 intsint *arr = new int[300]; // deallocate all 300 int locationsdelete [] arr;
9
Pointers and classes Use # 1
Dynamically allocate and de-allocate class objects Use # 2
Dynamically allocate and de-allocate storage used by class member variables
Why is this important? When execution exits a block within which an object is
declared, a destructor is called automatically. The default destructor takes care of returning memory to
the heap in usage # 1. In usage # 2 we must write our own destructor to return the
memory used by member variables, else it is lost to the program creating a memory leak.
10
Illustrating the Destructor
m e m be r1= m 1
B e fo re de s tro ying o bj Af te r de s tro ying o bj
m e m be r2m e m be r1= m 1 m e m be r2
m 2 m 2
O n he ap
\\
R e m ains o n the he ap with no thingpo int ing at i t .
Dynamic class from Ford & Topp (member 1 is data -- member 2 is a pointer)
11
The destructortemplate<typename T>class myVector // super simple vector class{public: // the constructor allocates the array myVector(int n=0) : dSize(n), data(NULL) { if (dSize > 0) data = new T(dSize); } // the destructor deallocates the array ~myVector() { if (data != NULL) delete [] data; }private: T *data; // the dynamically allocated array int dSize; // the size of the array};
12
Assignment & Copy Constructor C++ provides for every class, default …
destructor, assignment operator copy constructor
All of these are “shallow” They only copy or deallocate member variables, NOT what they “point to”
For all classes that contain member variables that point to dynamically allocated memory, we must create our own destructor, assignment operator and copy constructor
13
// object A has 10 data locations and B is a copy of A// (but BOTH point to the same dynamically allocated array)
myVector<int> A(10), B(A);
A B nSize data nSize data
dynamically allocated array (10 locations)
Shallow Copy
14
Copy Constructor / Overloaded Assignment Operator
15
30
y
(a ) In i ti a l i z a ti on : ti m e 24 y = x
15
30
x
0
0
y
(b) As s i gn m e n t: z = x
15
30
x
15
30
15
Copy Constructors
The Copy Constructor is called whenever ….
1)objects are passed as value parameters
2) an object is returned as the function value
3) an object is created and initialized with another object of the same class
16
Copy Constructor Example: expanded myVector class
Note: parameter MUST be a reference parameter, because the copy constructor itself is used in passing by value
template<typename T>myVector<T>::myVector(const myVector<T>& v): dSize(v.dSize){ data = new T[v.dSize]; // allocate space for(int i=0; i<dSize; i++) data[i] = v.data[i]; // copy values}
17
The Pointer ‘this’
*this objA;
this->memberl
18
Vectors vs. Lists Vector
Direct access to elements by index Dynamically resizable at end Push_back, pop_back, and [ ] are Θ(1) Insert, delete are Θ(n) because they require shifting of
elements (n/2 on average) List
Sequential access to all elements Resizable by addition anywhere in list Element access is Θ(n) requiring list traversal Insert & delete are Θ(1), once we have access to the
element
19
Model of a list object with links to next and previous elements
fro nt b ac k
20
The List ADT
The list API provides three constructors to declare a list object.
0 .00 .0 0 .00 .0 0 .00 .0 0 .00 .0(a) lis t< d o ub le> realL is t(8)
8 :3 08 :3 0 8 :3 08 :3 0 8 :3 0 8 :3 0(b ) lis t< tim e24> tim eLis t(6, 8:30)
ar ray
(c ) lis t< s tring> s trL is t(s trA rr, s trA rr+ 3)l is tve c to r
21
CLASS list Constructors <list>
list();Create an empty list. This is the default constructor.
list(int n, const T&value = T());Create a list with n elements, each having a specified value. If the value argument is omitted, the elements
are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T().
list(T *first, T *last);Initialize the list, using the address range [first, last).
22
CLASS list Operations <list>
T& back();Return the value of the item at the rear of the list. Precondition: The vector must contain at least one
element.
bool empty() const;Return true if the vector is empty, false otherwise.
T& front();Return the value of the item at the front of the list. Precondition: The vector must contain at least one
element.
23
CLASS list Operations <list>
void push_back(const T& value);Add a value at the rear of the list. Postcondition: The list has a new element at the
rear, and its size increases by 1.
void pop_back();Remove the item at the rear of the list. Precondition: The list is not empty. Postcondition: The list has a new element at the rear
or is empty.
24
CLASS list Operations <list>
void push_front(const T& value);Add a value at the front of the list. Postcondition: The list has a new element at the
front, and its size increases by 1.
void pop_front();Remove the item at the front of the list. Precondition: The list is not empty. Postcondition: The list has a new element at the front
or is empty.
int size() const;Return the number of elements in the vector.
25
CLASS list Operations <list>
iterator begin();Returns an iterator that references the first position
(front) of the list. If the list is empty, the iterator value end() is returned.
const_iterator begin();Returns a const_iterator that points to the first position (front) of a constant list. If the list is empty, the
const_iterator value end() is returned.
iterator end();Returns an iterator that signifies a location immediately out of the range of actual elements. A program must
not dereference the value of end() with the * operator.
26
CLASS list Operations <list>
iterator end();Returns an iterator that signifies a location immediately out of the range of actual elements. A program must
not dereference the value of end() with the * operator.
const_iterator end();Returns a const_iterator that signifies a location
immediately out of the range of actual elements in a constant list. A program must not dereference the value of end() with the * operator.
27
CLASS list Operations <list>
void erase(iterator pos);Erase the element pointed to by pos.Precondition: The list is not empty.Postcondition: The list has one fewer element.
void erase(iterator first, iterator last);Erase all list elements within the iterator range [first,
last].Precondition: The list is not empty.Postcondition: The size of the list decreases by the
number of elements in the range.
28
CLASS list Operations <list>
iterator insert(iterator pos, const T& value);Insert value before pos, and return an iterator pointing to the position of the new value in the list. The
operation does not affect any existing iterators.Postcondition: The list has a new element.
29
CLASS list::iterator Operations <list>
*:Accesses the value of the item currently pointed to by the iterator.
*iter;*iter;
++: Moves the iterator to the next item in the list. iter++;iter++;
--: Moves the iterator to the previous item in the list.
iter--;iter--;
==: Takes two iterators as operands and returns true when they both point at the same item in the list.
iter1 == iter2iter1 == iter2
!=: Returns true when the two iterators do not point at the same item in the list.
iter1 != iter2iter1 != iter2
30
Palindromes
Strings that read the same forwards and backwards
Spaces and punctuation are ignored
Approaches (using lists) 1 – Pop matching pairs of 1st & last chars 2 – Scan inward to middle, matching
corresponding outermost pairs
31
Palindrome (pop matches)// Assume spaces & punctuation removed// by caller; all letters in lower casetemplate<typename T>bool isPal_1(const list<T>& alist) { list<T> copyList; copyList = alist; while (copyList.size() > 0) { if (copyList.front() != copyList.back()) return false; // mismatch, not a palindrome // pop matching pair copyList.pop_front(); copyList.pop_back(); } return true; // all pairs matched & popped}
32
Palindrome (scan inward)// Assume spaces & punctuation removed// by caller; all letters in lower casetemplate<typename T>bool isPal_2(const list<T>& alist) { list<T>::iterator left, right; left = alist.begin(); right = alist.end(); right--; while (left != right) { if (*left != *right) return false; // mismatch, not a palindrome right--; // move right iter 1 posit left if (left == right) return true; // even length string, we’re done left++; // move left 1 posit right } return true; // all pairs matched & popped}
33
SeqSearch(no binary search possible here)
// search values [*first,*last)- sequentially
template<typename T>List<T>::iterator seqSearch ( list<T>::iterator& first, list<T>::iterator& last, const T& target){ list<T>::iterator iter = first; while((iter != last) && (*iter != target)) iter++; return iter;}
34
Inserting an element into a list
fro nt
Lis t o b je c t (a fte r)
fro nt re a r
Lis t o b je c t (b e fo re )
ne w E ltre a r
ite r
2 55937 2 9374
ite r4
35
Removing an element from a list
fro nt
Lis t o b je c t (a fte r)
fro nt re a r
Lis t o b je c t (b e fo re )
re a rite r
2 5937 2 593
ite r? ?
36
Ordered Lists
Values maintained in order by key Numerical Alphabetical Lexicographical
Insertion is a two step process Search for first list value (V) greater than or equal to the
new value Insertion of new value in front of list value (V)
Ordered List Examples Inserting Removing duplicates
37
Inserting in Ordered Listtemplate<typename T>void insertOrder (list<T>& orderedList, const T& item){ list<T>::iterator curr = orderedList.begin(), stop = orderedList.end();
// find insertion spot, 1st value >= item while((curr != stop) && (*curr < item)) curr++;
// insert item ahead of *curr orderedList.insert(curr, item);}
38
Removing Duplicates// assume list is ordered. This way we can jump curr// ahead as soon as we find *p != *currtemplate<typename T>void removeDups (list<T>& orderedList) { list<T>::iterator curr, p; curr = orderedList.begin(); while (curr != orderedList.end()) { p = curr; p++; while ((p != orderedList.end() && (*p == *curr)) // pass p, move p forward, and call erase orderedList.erase(p++); curr = p; }}
39
Curses Library
Allows “text-based” GUI
Curses or Ncurses for Unix/Linux/AIX Defined in “curses.h” header file
PDCurses (Public Domain Curses) for Dos/Windows
Supports mouse, keypads, color, buffered & non-buffered input, and some simple graphics (lines, boxes, etc)
40
Curses (Windows, etc)
Curses works by creating a window / screen / console data structure within which the program moves the cursor and performs output
A refresh() function updates the actual window / screen / console to reflect the logical one
See class web site for URLs of Curses resources / tutorials / examples/ etc.
41
Curses Basics – Hello World!#include<curses.h>int main(){ // always do this first initscr(); // initialize curses
// these are optional cbreak(); // input keys immediately available noecho(); // stop echoing inputs nonl(); // faster cursor movement clear(); // clear the screen // move cursor to location and output message // upper left screen corner is 0,0 move(12,5); // move cursor to row 12, column 5 printw(“Hello World\n”); // instead of cout << ... refresh(); // make it visible to us getch(); // wait for a key to be hit
endwin(); // release window back to shell return 0;}
42
CursesInitialization & Shutdown
Must use … initscr(); // first, to initialize curses endwin(); // last, to return screen to normal
Probably use … cbreak(); // enable direct keystroke access noecho(); // prevent echoing of input nonl(); // speeds cursor movement getmaxyx(stdscr,h,w); // get screen limits
Notes: stdscr is the default screen/window
43
CursesMoving cursor & I/O
Cursor & position move(y,x); // move cursor to line y, char posit x
Upper left of screen is at position 0,0 getyx(y,x); // returns current cursor location
I/O printw(“%d %s”, num, str); // cout <<
Parameters are same as printf() Prints in window from current cursor location
scanw(“%f”,&realNum); // cin >> Parameters are same as scanf() Reads input stream as directed by format string
getch(); // reads single keystroke wgetch(stdscr); // reads single keystroke
From indicated window
44
CursesMoving cursor & I/O
Combined movement and I/O mvprintw(y,x,“%d %s”, num, str);
Prints in window from cursor location y, x mvscanw(y,x,“%f”,&realNum);
Reads input stream as directed by format string
Making it visible refresh(); // updates console
Makes it match logical window data structure
45
Curses Color Only supported on certain terminal types (xterm is good bet)
set term=xterm at unix prompt before running program Or run in separate xterm using ...
xterm -geometry 80x50 -e tttCM start_color(); // initializes color init_pair(num,fg_color,bg_color); Creates a COLOR_PAIR of foreground and background colors xterm has 8 colors and 64 color pairs, max attron(COLOR_PAIR(num));
makes color pair the output attribute until turned off attroff(COLOR_PAIR(num));
46
Curses COLORS
COLOR_BLACK COLOR_RED COLOR_GREEN COLOR_YELLOW COLOR_BLUE COLOR_MAGENTA COLOR_CYAN COLOR_WHITE
47
Curses Using Special Keys
To use function keys, arrows, etc. Initialize with keypad(stdscr,TRUE); Must be used before both special key input And before mouse event capture prevents immediate translation to ASCII
Get key with int key = getch();
Test key with keycodes KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT KEY_HOME, KEY_BACKSPACE KEY_F(n) Function keys, for 0 <= n >= 63 KEY_DC Delete character KEY_IC Insert char or enter insert mode KEY_ENTER Enter or send
48
Curses Mouse Support
Only in Ncurses (Linux & AIX) Initialize for capture of events with
mousemask(ALL_MOUSE_EVENTS,NULL); Specific masks (BUTTON1_CLICKED, ...)
Read mouse click with int key = getch(); or int key = wgetch(stdscr);
49
Curses Mouse Events Processing event
Value read will equal KEY_MOUSE Use getmouse(&event) to recover event from event queue Use event.bstate to determine button state (which event) Use event.x and event.y to determine where mouse was clicked in
window
MEVENT event; // data type for mouse event
int key = getch();
if (key == KEY_MOUSE) // if it was the mouse if (getmouse(&event) == OK) { // get the event if (event.bstate & BUTTON1_CLICKED) { // so do something with // event.y; and event.x;
50
Example Curses Programs(on class web site) hi.cpp
Simple curses “Hello World” program bigHi.cpp
Shows big “HI” on screen hw4_sample.cpp
Shows size of curses window, once open mvbox.cpp
Moving star shape: Uses keyboard (letter) inputs mvbox2.cpp
Moving star and box shapes: Uses keyboard (letter) inputs mvbox3.cpp
Like mvbox2.cpp but also uses arrow keys and mouse tttCM (executable only)
TicTacToe: Uses Color, mouse and keyboard inputs