Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009



Similar documents
Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

The C Programming Language course syllabus associate level

Object Oriented Software Design II

El Dorado Union High School District Educational Services

C++FA 5.1 PRACTICE MID-TERM EXAM

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

An Incomplete C++ Primer. University of Wyoming MA 5310

CISC 181 Project 3 Designing Classes for Bank Accounts

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Node-Based Structures Linked Lists: Implementation

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

C++ Programming Language

Binary Heap Algorithms

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

C++ Overloading, Constructors, Assignment operator

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Testing and Inspecting to Ensure High Quality

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Linked Lists: Implementation Sequences in the C++ STL

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Software Engineering Techniques

Object Oriented Software Design II

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Sequences in the C++ STL

Variable Base Interface

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

CS A250 (CRN ) C++ Programming Language 2. Syllabus Fall 2015

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Help on the Embedded Software Block

Linked List as an ADT (cont d.)

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Glossary of Object Oriented Terms

CS193j, Stanford Handout #10 OOP 3

Jorix kernel: real-time scheduling

N3458: Simple Database Integration in C++11

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Module 6: How to Write the Book

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

5 Arrays and Pointers

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

ECS 165B: Database System Implementa6on Lecture 2

A deeper look at Inline functions

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Course Syllabus. COSC 1437 Programming Fundamentals II. Revision Date: August 21, 2013

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Marketing Best Practices - Top 10 tips

Loop Invariants and Binary Search

Ch 7-1. Object-Oriented Programming and Classes

Exploratory Testing in an Agile Context

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Recursion vs. Iteration Eliminating Recursion

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

C++ INTERVIEW QUESTIONS

Cours de C++ Utilisations des conteneurs

16 Collection Classes

Constructor, Destructor, Accessibility and Virtual Functions

Introduction to Programming System Design. CSCI 455x (4 Units)

EP241 Computer Programming

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

arrays C Programming Language - Arrays

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Club Accounts Question 6.

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

13 Classes & Objects with Constructors/Destructors

Lecture Notes on Linear Search

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

CS 145: NoSQL Activity Stanford University, Fall 2015 A Quick Introdution to Redis

Classes: Relationships Among Objects. Atul Prakash Background readings: Chapters 8-11 (Downey)

Virtual Flips QUICK Start Guide

Computer Programming I

Study of a neural network-based system for stability augmentation of an airplane

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Software testing. Objectives

How To Develop An Application

Leak Check Version 2.1 for Linux TM

Why you shouldn't use set (and what you should use instead) Matt Austern

Book 3 Cost Estimating in an Agile Development Environment. (early release)

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

1 Abstract Data Types Information Hiding

OpenCL Static C++ Kernel Language Extension

Towards practical reactive security audit using extended static checkers 1

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Binary storage of graphs and related data

Transcription:

Software Engineering Concepts: Testing Simple Class Example continued Pointers & Dynamic Allocation CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G. Chappell

Unit Overview Advanced C++ & Software Engineering Concepts Major Topics: Advanced C++ Parameter passing Operator overloading The structure of a package Silently written & called functions Pointers & dynamic allocation Managing resources in a class Templates Containers & iterators Error handling Introduction to exceptions Introduction to Linked Lists Major Topics: S.E. Concepts Abstraction Invariants Testing Some principles 14 Sep 2009 CS 311 Fall 2009 2

Review Software Engineering Concepts: Invariants [1/2] An invariant is a condition that is always true at a particular point in an algorithm. Special kinds Precondition. An invariant at the beginning of a function. The responsibility for making sure the preconditions are true rests with the calling code. What must be true for the function to execute properly. Postcondition. An invariant at the end of a function. Tells what services the function has performed for the caller. Describe the function s effect using statements about objects & values. Class invariant. An invariant that holds whenever an object of the class exists, and execution is not in the middle of a public member function call. Statements about data members that indicate what it means for an object to be valid or usable. Notes The above form the basis for operation contracts. The job of a constructor is to make the class invariants true. 14 Sep 2009 CS 311 Fall 2009 3

Review Software Engineering Concepts: Invariants [2/2] Write reasonable class invariants for the following class. // class Foo // Invariants: // v.size() >= 6. class Foo { public: // bar // Pre: None. bar() { v[5] = 0; } private: std::vector<int> v; }; Statements about data members that indicate what it means for an object to be valid or usable. 14 Sep 2009 CS 311 Fall 2009 4

Software Engineering Concepts: Testing A Tragic Story [1/4] Suppose you are writing a software package for a customer. The project requires the writing of four functions. double foo(int n); // gives ipsillic tormorosity of n void foofoo(int n); // like foo, only different int bar(int n); // like foofoo, only more different char barbar(int n); // like bar; much differenter So, you get to work. You start by writing function foo 14 Sep 2009 CS 311 Fall 2009 5

Software Engineering Concepts: Testing A Tragic Story [2/4] after a huge amount of effort, the deadline arrives. But you are not done. However, you do have three of the four functions written. Here is what you have. double foo(int n) { [amazingly clever code here] } void foofoo(int n) { [stunningly brilliant code here] } int bar(int n) { [heart-breakingly high-quality code here] } // Note to self: write function barbar. 14 Sep 2009 CS 311 Fall 2009 6

Software Engineering Concepts: Testing A Tragic Story [3/4] You meet with the customer. You explain that you are not done. The customer is a bit annoyed, of course, but he knows that schedule overruns happen in every business. So, he asks, Well, what have you finished? What can it do? Unfortunately, you do not have all the function prototypes in place. Thus your unfinished package, when combined with the code that is supposed to use it, does not even compile, much less actually do anything. You tell the customer, Um, actually, it can t do anything at all. Do you want to see my beautiful code? you ask. No, replies the customer, through clenched teeth. The customer storms off and screams at your boss, who confronts you and says you had better have something good in a week. You solemnly assure them that this will happen. You go back to work 14 Sep 2009 CS 311 Fall 2009 7

Software Engineering Concepts: Testing A Tragic Story [4/4] and you write a do-nothing function barbar, just to get things to compile. However, when you do this, you realize that, since you have never done a proper compile of the full package, you have never really done a proper test of the first three functions. Now that you can test them, you find that they are full of bugs. Alas, you now know that you have been far too optimistic; nothing worthwhile is going to get written in the required week. You begin practicing your lines for your exciting new career: Do you want fries with that? Do you want fries with that? Do you want fries with that? Do YOU want FRIES with THAT? 14 Sep 2009 CS 311 Fall 2009 8

Software Engineering Concepts: Testing Lessons Observations Code that does not compile is worthless to a customer, even if it is nearly done. It might not be worth anything to anyone; you can t tell, because Code that does not compile cannot be tested, and so it might be much farther from being done than you suspect. Testing is what uncovers bugs. Conclusion First priority: Get your code to compile, so that it can be tested. A Revised Development Process Step 1. Write dummy versions of all required modules. Make sure the code compiles. Step 2. Fix every bug you can find. Not having any code in the function body is a bug. Write notes to yourself in the code. Make sure the code works. Step 3. Put the code into finished form. Make it pretty, well commented/documented, and in line with coding standards. Many comments can be based on notes to yourself. Make sure the code is finished. 14 Sep 2009 CS 311 Fall 2009 9

Software Engineering Concepts: Testing Try Again [1/3] Suppose you had used this revised development process earlier. Step 1. Write dummy versions of all required modules. double foo(int n) // gives ipsillic tormorosity of n {} // WRITE THIS FUNCTION!!! void foofoo(int n) // like foo, only different {} // WRITE THIS FUNCTION!!! int bar(int n) // like foofoo, only more different {} // WRITE THIS FUNCTION!!! char barbar(int n) // like bar; much differenter {} // WRITE THIS FUNCTION!!! Does it compile? No. My compiler says foo, bar, barbar must each return a value. 14 Sep 2009 CS 311 Fall 2009 10

Software Engineering Concepts: Testing Try Again [2/3] Continuing Step 1. Add dummy return statements. double foo(int n) // gives ipsillic tormorosity of n { return 1.; } // WRITE THIS FUNCTION!!! void foofoo(int n) // like foo, only different {} // WRITE THIS FUNCTION!!! int bar(int n) // like foofoo, only more different { return 1; } // WRITE THIS FUNCTION!!! char barbar(int n) // like bar; much differenter { return 'A'; } // WRITE THIS FUNCTION!!! Does it compile? Yes. Step 1 is finished. 14 Sep 2009 CS 311 Fall 2009 11

Software Engineering Concepts: Testing Try Again [3/3] Step 2. Fix every bug you can find. You begin testing the code. Obviously, it performs very poorly. But you begin writing and fixing. And running the code. So when something does not work, you know it. When you figure something out, you make a note to yourself about it. As before, the deadline arrives, but the code is not finished yet. You meet with the customer. The project is not finished, you say, but here is what it can do. You estimate how long it will take to finish the code. You can make this estimate with confidence, because you have a list of tests that do not pass; you know exactly what needs to be done. 14 Sep 2009 CS 311 Fall 2009 12

Software Engineering Concepts: Testing Development Methodologies Software-development methodologies often include standards for how code should be tested. In particular, see, Test-Driven Development. Many people recommend writing your tests first. Each time you add new feature, you first write tests (which should fail), then you make the tests pass. When the finished test program runs without flagging problems, Step 2 is done. Pretty up the code, and it is finished. We will use a variation on this in the assignments in this class. I will provide the (finished) test program. However, when you turn in your assignment, I act as the customer; I do not want to see code that does not compile. 14 Sep 2009 CS 311 Fall 2009 13

Simple Class Example Write Some More continued Last time, we began writing a simple class whose objects store a time of day, in seconds: class TimeSec. TO DO In class, we put together the general structure of the package. I have added getter & setter functions and another ctor. Now we continue Write the member functions and associated global operators for class TimeSec. Apply the software engineering concepts just discussed. Done. See timesec.h, timesec.cpp, on the web page. 14 Sep 2009 CS 311 Fall 2009 14

Simple Class Example Notes [1/2] Note 1: External interface does not dictate internal implementation (although it certainly influences it). Class TimeSec deals with the outside world in terms of hours, minutes, and seconds. However, it has only one data member, which counts seconds. Note 2: Avoid duplication of code. Look at the two operator++ functions. We could have put the incrementing code into both of them, but we did not. Why is this a good thing? 14 Sep 2009 CS 311 Fall 2009 15

Simple Class Example Notes [2/2] Note 3: There are three ways to deal with the possibility of invalid parameters. Insist that the parameters be valid. Use a precondition. Allow invalid parameter values, but then fix them. If invalid parameter values are passed, signal the client code that there is a problem. We will discuss this further when we get to Error Handling. Responsibility for dealing with the problem lies with the code executed before the function. in the function. after the function. Look at the three-parameter constructor. Which solution was used there? 14 Sep 2009 CS 311 Fall 2009 16

Pointers & Dynamic Allocation Introduction A pointer is a variable that contains the memory address of another variable. The type of a pointer is the type of the value it points to, followed by *. int n; // int int * ip1; // pointer to int ip1 n Get the address of a variable using the address-of operator ( & ). This operator returns a pointer to the variable. ip1 = &n; ip1 n Get the value pointed to using the dereference operator ( * ). This operator returns a reference to the value the pointer points to. cout << *ip1; // outputs n *ip1 = 3; // sets n to 3 14 Sep 2009 CS 311 Fall 2009 17

Pointers & Dynamic Allocation Copying and Null We can copy and assign pointers. This does not copy the value pointed to. int * ip2 = ip1; ip1 n ip2 A special value: the null pointer, represented by 0 used as a pointer. Why? This is a value we can test for. Alternative: NULL, defined in <iostream>. ip1 = 0; if (ip1 == 0) ip1 = NULL; ip1 n ip2 Do not dereference a null pointer or an uninitialized pointer. cout << *ip1; // BAD!! ip1 is null int * ip3; *ip3 = 2; // BAD!! ip3 is uninitialized 14 Sep 2009 CS 311 Fall 2009 18

Pointers & Dynamic Allocation Pointer Arithmetic [1/2] The name of an array can be used as ( decays to ) a pointer to the first item in the array. int iarr[7]; ip1 = iarr; ip1 = &(iarr[0]); // Same as above ip1 iarr When a pointer points to an array item, we can increment and decrement the pointer to move it to the next and previous items in the array. ++ip1; ip1 iarr Do not increment or decrement a pointer that does not point to an array. 14 Sep 2009 CS 311 Fall 2009 19

Pointers & Dynamic Allocation Pointer Arithmetic [2/2] Once you understand that, other arithmetic operators act as expected. ip1 += 3; // advances ip1 by 3 ip1 -= 2; // moves ip1 back 2 iarr ip2 = ip1 + 4; // points ip2 to the item 4 past ip1's item // does not change ip1 std::ptrdiff_t d = ip2 ip1; // Sets d to 4 Only subtract pointers that point to the same array. You can also use a pointer as if it is an array. cout << ip1[2]; // same as *(ip1+2) 14 Sep 2009 CS 311 Fall 2009 20 ip1

Pointers & Dynamic Allocation Dynamic Allocation [1/2] The new operator allocates and constructs a value of a given type and returns a pointer to it. ip1 = new int; ip1 We can add constructor parameters. ip1 = new int(2); When we do dynamic allocation, we must deallocate using delete on a pointer (any pointer) to the dynamic value. delete ip1; // destroys the int; does not affect ip1 Do not depend on the destructor of the pointer to do this. The destructor of a pointer does nothing. When dynamic memory is never deallocated, we have a memory leak. 14 Sep 2009 CS 311 Fall 2009 21

Pointers & Dynamic Allocation Dynamic Allocation [2/2] The new and delete operators also have array forms. ip1 = new int[4]; ip1 delete [] ip1; Notes You cannot specify constructor parameters in the array version of new. Array items are always default constructed. Do not mix array & non-array versions. Use the proper form of delete for each new. 14 Sep 2009 CS 311 Fall 2009 22