CSC265 - Inspection 3

Program state and state invariants

Old terms

Specification
a precise description of the required observable behavior of a program
Implementation
source or object code
Verification
showing that code or documentation is correct
Fault
an error in source code or documentation
Inspection
human review of source code with the goal of revealing faults

New terms

Program state

Definition
The state of a program P at time T is the values of all the data storage associated with P at time T. Note: includes dynamically allocated storage.
State representation
Critical that there is a convenient method available. There are two main possibilities: vector and diagram.
Vector representation
List the values for each variable in the order in which the variables are declared. For example, in swap we can represent a state as a pair of integers, such as (3,5). Note: inadequate for programs that use dynamic storage.
Diagram representation
Draw a diagram representing the storage structure and values. Note: useful for programs that use linked lists. Actual addresses need not be known; arrows can be used instead. Example: LineStorage
		line 0: "A", "B", "C"
		line 1: "D", "E"
Diagram on black board.
Bottom line
It is critical that you learn to communicate effectively about program states: their values and their representation.

State invariant

Definition
intuition
Example: LineStorage.c

Partial Module Implementation: LineStorage.h

/********** LineStorage module---implementation **********/

#include 
#include 
#include "kwic.h"
#include "LineStorage.h"

/***** local constants *****/

/***** local types *****/

/*Each line is a list of WordNodes.*/
typedef struct WordNode {
	char* word;
	struct WordNode* nextWordPtr;
} WordNode;
typedef WordNode* WordNodePtr;

/*The LineStorage module stores a list of LineNodes*/
typedef struct LineNode {
	struct LineNode* nextLinePtr;
	WordNodePtr headWordPtr;
	WordNodePtr tailWordPtr;
	int wordCount;
} LineNode;
typedef LineNode* LineNodePtr;

/***** local variables *****/

static LineNodePtr headLinePtr, tailLinePtr;
static int lineCount;

/***** state invariant *****

1. if lineCount == 0 then
	headlinePtr == NULL
	taillinePtr == NULL
   else
	headLinePtr points to a null-terminated linked list of LineNodes.
	tailLinePtr points to the last LineNode in this list.
	There are lineCount LineNodes in this list.

2. for every LineNode allocated by LineStorage
	if wordCount == 0 then
	    headWordPtr == NULL
	    tailWordPtr == NULL
	else
	    headWordPtr points to a null-terminated linked list of WordNodes.
	    tailWordPtr points to the last WordNode in this list.
	    There are wordCount WordNodes in this list.

3. For every WordNode allocated word is a null-terminated array of characters.

4. All of the dynamic memory allocated by LineStorage (and not yet freed)
   is in the list structure headed by headLinePtr.
*/

/***** local functions *****/

/*
...
*/

/***** exported functions *****/

void LSInit(void)
{
	headLinePtr = NULL;
	tailLinePtr = NULL;
	lineCount = 0;
}

KWStatus LSAddLine(void)
{
	LineNodePtr newLinePtr;

	/* create and fill a LineNode */
	newLinePtr = malloc(sizeof(LineNode));
	if (newLinePtr == NULL)
		return KWMEMORYERROR;
	lineCount++;
	newLinePtr->nextLinePtr = NULL;
	newLinePtr->headWordPtr = NULL;
	newLinePtr->tailWordPtr = NULL;
	newLinePtr->wordCount = 0;

	/* link in the new LineNode */
	if (tailLinePtr == NULL) {
		headLinePtr = newLinePtr;
	} else {
		tailLinePtr->nextLinePtr = newLinePtr;
	}
	tailLinePtr = newLinePtr;
	return KWSUCCESS;
}



Previous Section Back to Index Next Section