CPSC 427: Object-Oriented Programming
Michael J. Fischer
Storage Management (continued)
Static
data
members
A static data member belongs to the class itself rather than to
instantiations of the class. Thus, all instantiations share the static
member. Moreover, it exists even before the class has been instantiated.
A static class data member must be declared and defined.
Example from brackets demo
In token.hpp, variable brackets is defined to be a static constant
string:
static const string brackets;
It is initialized in token.cpp:
const string Token::brackets = "[](){}<>";
Static data member example
Here’s a case where a non-const static data member is useful. In
debugging a program with many objects of the same type, it is
often useful to be able to distinguish the objects in diagnostic
printouts.
Using a static data member nextID, here’s how to give each new object a unique ID:
Class definition
In the .cpp file, you would place the line:
Static function members
Function class members can also be declared static.
A static function can be called before the class has been instantiated. Example: Suppose MyClass defines
Static class functions are global
Ways in which static class functions are like global C-style functions:
Ways in which they differ:
Debugging memory management errors
Memory management errors can be particularly difficult to debug
because they often lead to bizarre symptoms that mislead debugging
efforts.
Debugging tools such as gdb have their place, but be aware that a
program with memory management errors may behave differently when
run under a debugger than when run alone.
I’ll give a brief overview of several kinds of errors that can happen, what causes them, and what to do about them. Read chapter 6 of the textbook for a much fuller discussion.
Five common kinds of failures
Memory leak
A memory leak is when storage that has been allocated to a process
becomes inaccessible.
Symptoms are that the process’s memory footprint becomes larger
and larger over time, eventually leading the process to become
very slow as useless data gets swapped in and out of physical
memory.
The tool valgrind will catch many kinds of memory errors, including
memory leaks.
To use, type valgrind myapp arg1 arg2 …. This runs myapp arg1 arg2 … under its control.
Amnesia
“I stored 7 in x. Why did cout << x print 356991?”
Answer: “Somebody changed it between the time you stored into x and when you printed it.” A few of many possible reasons:
Segmentation fault
From the system’s perspective, a memory reference can be one of three kinds:
Segmentation faults often result from attempting to dereference an invalid pointer, as in the following:
Bus error
Bus errors are less common in modern operating systems. Dereferencing
0 on older systems often caused bus errors.
They can still happen in various situations, and you may occasionally still see them. The program mistakes that cause them are pretty much the same as the ones that cause segmentation faults.
Waiting for eternity
“I wrote cout << x; , but nothing prints. Why?
Some possibilities:
What kinds of program errors cause these problems?
What makes these problems hard to diagnose?
The effects of memory management errors often are only observed
long after the original error, and often in an unrelated piece of
code.
References to unowned memory may return different values on different
runs of the system, leading to seemingly random behavior.
They may also return different values when run in a different
environment, such as under the control of gdb or valgrind.
Seemingly random behavior is almost always an indication of memory errors in your program. Finding the cause of the error requires methodical tracing of control flow through your program.
Bar Graph Demo
Overview
of
bar
graph
demo
These slides refer to demo 08-BarGraph.
This demo reads a file of student exam scores, groups them by deciles,
and then displays a bar graph for each decile.
The input file has one line per student containing a 3-letter student code followed by a numeric score.
Scores should be in the range [0,100]
Overview
(cont.)
The output consists of one line for each group listing all of the students
falling in that group. An 11th line is used for students with invalid
scores.
Sample output:
Method
Each student is represented by an Item object that consists of the
initials and a score.
The program maintains 11 linked lists of Item, one for each bar of the
graph. A bar is represented by a Row object.
For each line of input, an Item is constructed, classified, and inserted
into the appropriate Row.
When all student records have been read in, the bars are printed.
A Graph object contains the bar graph as well as the logic for creating a bar graph from a file of scores as well as for printing it out.
Analysis of 08-BarGraph demo