Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.

Slides:



Advertisements
Similar presentations
Computer Science 2212a/b - UWO1 Structural Testing Motivation The gcov Tool An example using gcov How does gcov do it gcov subtleties Further structural.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Chapter 10.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Software and Software Vulnerabilities. Synopsis Array overflows Stack overflows String problems Pointer clobbering. Dynamic memory management Integer.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CSc 352 C : Arrays, Structs, Pointers
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Computer Security and Penetration Testing
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays and Strings in Assembly
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Computer Programming for Engineers
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
C Strings Doing strings the old fashioned way. strings vs c-strings C++ strings are an object data type – State : list of characters – Can ask it to perform.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSC 482/582: Computer Security
Computer Organization and Design Pointers, Arrays and Strings in C
Computer Science 210 Computer Organization
Motivation and Overview
C Programming Tutorial – Part I
Computer Science 210 Computer Organization
Format String.
CSc 352: Testing and Code Coverage
Chapter 16 Pointers and Arrays
Exercise Arrays.
Chapter 9: Pointers and String
Testing & Security Dr. X.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial credit. See the web page for details. Late assignments will not be accepted, so please turn in early, turn in often, and double check your work. 1

CSc 352: Testing and Code Coverage

Testing and test cases int main() { read x; if (x is odd) { compute payroll data; } else { delete all files; send rude to boss; crash computer; } 3 make sure you use at least fifty different test inputs 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, … It isn’t enough to have a lot of test cases. We have to make sure our tests “cover” the program adequately.

gcov: a code coverage analyzer 4 test coverage program – indicates how many times each line was executed – marks code that did not get executed – cumulative over a set of tests helps you understand – how effectively your current test cases “cover” the code – what additional test inputs you need in order to get better coverage needs the program to be compiled with additional gcc options

testing didn’t execute all of the code An example 5 input additional compiler flags

testing didn’t execute all of the code An example 6 input

An example 7 input

An example 8 input

Code coverage and testing Just because every line has been executed does not mean the program has been tested thoroughly – we may want to test the same line of code under different conditions e.g.: a loop should be tested with values that cause 0, 1, and “many” iterations However, if some lines are not executed the program is definitely not thoroughly tested – gcov helps us identify and fix this exception: “system errors” that may be difficult to create 9

Example of not enough testing 10 This is (almost) the program we wrote in class to convert all lower case letters in a string to upper case.

Example of not enough testing 11 Compile and test it. 100% of code is executed and the result is correct. So the code has no bugs, right?

Example of not enough testing 12 Compile and test it. 100% of code is executed and the result is correct. So the code has no bugs, right? Oops! There were still errors.

gcov: summary code coverage testing tool – works with gcc; needs additional compiler flags gcc –fprofile-arcs –ftest-coverage … shows execution frequency of each line of code – reports % of lines covered by tests coverage values are cumulative delete *.gcda file to start afresh – how many times each line was executed – highlights lines not executed 13

Pointers A pointer in C holds the memory address of a value – the value of a pointer is an address – the value of the memory location pointed at can be obtained by “dereferencing the pointer” (retrieving the contents of that address) memory locationaddress pointer dereferencing: * p p

C pointers vs. Java references C pointers a pointer is the address of a memory location – no explicit type information associated with it arithmetic on pointers is allowed, e.g.: *(p+27) Java references a reference is an alias for an object – references have associated type information arithmetic on references not allowed 15

Declaring pointer variables Two new operators (unary, prefix): – & : “address of” * : “dereference” Declarations: – a pointer x to something of type T is declared as T *x Example: int *p; // p: pointer to an int char **w; // w: pointer to a pointer to a char 16

Using pointer-related operators If x is a variable, &x is the address of x If p is a pointer, *p is the value of whatever points to *(&p)  p always 17

Arrays An array in C is just a contiguous sequence of memory locations – size of each element depends on type – the length of the array is not part of the array type – the language does not require that array accesses be checked to be within the array bounds out-of-bound accesses result in bugs, security flaws (“buffer overflow vulnerabilities”) 18

More arrays Consider an array declared as: int A[20]; – the value of A[ i ] is the contents of the memory location occupied by element i of A; – the value of A is the address of the array A, i.e., &(A[0]); this does not have size information associated with it. 19

More arrays To pass an array as an argument to a function, you pass the array name – since the value of the array name is the address of the array, what is actually passed is a pointer to the array This does not have size information associated – the called function does not know how big the array is – need to provide a mechanism for callee to figure this out: either pass the size of the array separately; or terminate the array with a known value (e.g., 0) 20

scanf() and pointers To read input using scanf(), we have to provide: – a format string with conversion specifications (%d, %s, etc.) that says what kind of value is being read in; and – a pointer to (i.e., the address of) a memory area where the value is to be placed Reading in an integer: int x; scanf(“%d”, &x); // &x  address of x Reading in a string: char str[…]; scanf(“%s”, str); // str  address of the array str 21

Example 1 22 str_rev is a function of type “char *”, i.e., returns a pointer to a character the argument is an array (its size is not part of its type) string library functions array  pointer

Example 1… 23 figure out where the ‘\0’ is use this to control how many array elements to processes

Example 1… 24

Example 2: string reversal using pointers 25 array  pointer

Example 2… 26 abcd\0 s tptr

Example 2… 27