CS 103 Lab The Files are *In* the Computer

Similar documents
EE355 Lab 5 - The Files Are *In* the Computer

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

EE 355 Lab 4 - Party Like A Char Star

CS61, Fall 2012 Section 2 Notes

CS 103 Lab 6 - Party Like A Char Star

CS 103 Lab - Party Like A Char Star

CSE 333 Midterm Exam Sample Solution 7/28/14

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

TI2725-C, C programming lab, course

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS103L SPRING 2018 UNIT 7: FILE I/O

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Use Dynamic Analysis Tools on Linux

COP Programming Assignment #7

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

EE 355 PA4 A* Is Born

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Recitation 10: Malloc Lab

CS61C : Machine Structures

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

Both parts center on the concept of a "mesa", and make use of the following data type:

Debugging (Part 2) 1

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

CS 11 C track: lecture 5

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

HW1 due Monday by 9:30am Assignment online, submission details to come

Scientific Programming in C IX. Debugging

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Programs in memory. The layout of memory is roughly:

Recitation: C Review. TA s 20 Feb 2017

Lab 4: Bash Scripting

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

A Capacity: 10 Usage: 4 Data:

CS61C : Machine Structures

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Creating a String Data Type in C

In either case, remember to delete each array that you allocate.

Intermediate Programming, Spring 2017*

ECE551 Midterm Version 1

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Class Information ANNOUCEMENTS

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Lecture 14 Notes. Brent Edmunds

CSE 15L Winter Midterm :) Review

CS61C : Machine Structures

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Programming Tips for CS758/858

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

CS 103 The Social Network

Vector and Free Store (Pointers and Memory Allocation)

Lecture Notes on Garbage Collection

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Recitation 2/18/2012

ECS 153 Discussion Section. April 6, 2015

CS354 gdb Tutorial Written by Chris Feilbach

The Art and Science of Memory Allocation

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Lab 4: Shell Scripting

Starting to Program in C++ (Basics & I/O)

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Homework #1 From C to Binary

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Book keeping. Will post HW5 tonight. OK to work in pairs. Midterm review next Wednesday

CS24 Week 3 Lecture 1

Lab 7 Unit testing and debugging

Structure of Programming Languages Lecture 10

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Arrays and Memory Management

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 241 Data Organization Binary Trees

Should you know scanf and printf?

How to approach a computational problem

6.S096: Introduction to C/C++

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

Chapter 17 vector and Free Store

pointers + memory double x; string a; int x; main overhead int y; main overhead

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Praktische Aspekte der Informatik

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I


CSE 333 Lecture 2 - arrays, memory, pointers

Homework #3 CS2255 Fall 2012

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

Transcription:

CS 103 Lab The Files are *In* the Computer 1 Introduction In this lab you will modify a word scramble game so that instead of using a hardcoded word list, it selects a word from a file. You will learn how to utilize the valgrind tool to check for run-time errors in a program and double-check your program s correctness using this tool. 2 What you will learn After completing this lab you should be able: Open and read text files using an ifstream object Use fail() to gracefully handle input error conditions Dynamically allocate arrays (and deallocate them) Review deep vs. shallow copy Use valgrind to find memory leaks and other memory issues 3 Background Information and Notes Note: The lecture notes will be a good source of information on how to understand arrays of char pointers and deep vs. shallow copying, which is useful for this lab. In this lab, we ll give you already-working code for a word scramble game (details will be given later). You will change the wordbank array hard-coded into the program, and instead allow the user to specify the filename of a text file that contains the words for the word bank. For instance, if wordbank.txt is a file containing a list of words, the user would run the program by passing that file name as a command line argument: $./scramble wordbank.txt The wordbank file format shall be as follows. The first line shall contain an integer that indicates how many text words follow it. Then exactly that many words should follow separated by some sort of whitespace. See below for an example. (Notice that, in keeping with how >> for cin and ifstreams works, the words can either be on the same line or separate lines as long as there is whitespace between them. Last Revised: 10/20/2017 1

6 cs103 trojan midterm aced perfect score wordbank.txt 4 Procedure 4.1 Starter Files Start by getting the files for this week: $ cd ~/Dropbox/cs103 # or wherever you like $ mkdir lab-valgrind $ cd lab-valgrind $ wget http://bytes.usc.edu/files/cs103/lab-valgrind.tar $ tar xvf lab-valgrind.tar Open up scramble.cpp in gedit. The given code runs a game that works as follows: It defines a built-in array wordbank of C strings, picks a random string from that array randomly rearranges its letters gives the user 10 guesses to figure out the original, unscrambled string You should understand the given code before proceeding (though don t worry if you aren t sure of the math underlying the Knuth shuffle function permute(), since you won t need to change that. 4.2 Reading Word Bank from a File Your goal with scramble.cpp will be to alter it by deleting the built-in words list, instead using any list of words given in a file whose name the user will provide. For example $./scramble wordbank.txt # play game using this word list $./scramble palabras.txt # play game in Spanish To get this to work, you should do the following. 1. Include the appropriate file stream header file 2. Delete the global wordbank array declaration and numwords declaration 2 Last Revised: 10/20/2017

3. Modify the definition of main() to allow for command line arguments to be accessed and add a check to ensure the user has entered enough command line arguments (i.e. provided something that can be used as the filename of their wordbank) 4. Add code to open the file using the command line argument provided as the filename. Check to ensure you could successfully open the file. If you fail to open the file, just output a nice error message and quit the program. 5. Attempt to read an integer from the file which is the number of subsequent words in the file. If you fail to read an integer successfully, close the file and exit gracefully with an appropriate message to the user. 6. Dynamically allocate an array of char*'s named 'wordbank' (one per word that you expect to read from the file) that will point at each character array (i.e. word) that you are about to read in. 7. Declare a character array (a.k.a 'buffer') of 41 characters max to hold the strings that you read in (one by one) from the file [I.e., you may assume the maximum word length will be 40 characters]. 8. In a loop, read in each word from the file into the character array you just allocated, then dynamically allocate a new character array that is just the right size to hold that word. Copy the word from the buffer to your new array, and make sure your wordbank has a copy of the pointers to the array you just allocated. a. The dynamically allocated memory should be exactly as big as needed. So while the buffer that temporarily holds the word will be 40 bytes large, the dynamically-allocated memory where the words end up should be significantly smaller. An example of this is given in the lecture notes. 9. Close your input file. The original word scramble code should be able to be reused for everything else (make sure variable names used in your code match those needed for the remainder of the code). 10. At the end of main deallocate all the memory you allocated with new. Compile your code and test it in English and Spanish as mentioned above. Next we ll introduce a tool you will use to check your program for bugs. 4.3 Introduction to Valgrind Just like gdb can be used to help us examine and track down segmentation faults in a program, valgrind is a helpful tool to track down memory allocation and usage errors. It will run your program in a special way and tell you if you misuse memory or forget to deallocate it (it also will find other issues, like trying to use garbage values or accessing arrays out of bounds). In this lab we ll be running it like so: Last Revised: 10/20/2017 3

$ valgrind --tool=memcheck --leak-check=yes./scramble wordbank.txt You can see that we are passing a couple of options, then writing the command we want to inspect (including its arguments). If you used memory appropriately and deallocated everything you should get a summary at the end that looks like this. Note the line that says 10 allocs and 10 frees. This means you dynamically allocated 10 blocks of memory and freed 10. Those numbers should match. If they don't, there was a memory leak. ==8146== ==8146== HEAP SUMMARY: ==8146== in use at exit: 0 bytes in 0 blocks ==8146== total heap usage: 10 allocs, 10 frees, 8,856 bytes allocated ==8146== ==8146== All heap blocks were freed -- no leaks are possible ==8146== ==8146== For counts of detected and suppressed errors, rerun with: -v ==8146== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) On the other hand, here s an example of what valgrind s output would be if there were an error. Suppose you forget to deallocate the words from the wordbank; here I allocated 10 blocks but only freed 4, which is bad, 6 blocks were not freed! Valgrind uses the the same debug symbols as gdb (the -g option that is baked into compile) to even show you which line of code allocated the memory that was never freed (look for the bold type below). It says main() line 48 was where the 'new' operator was used to allocate some memory that was never deleted. This should help you track down what you need to do to delete that memory. HEAP SUMMARY: in use at exit: 40 bytes in 6 blocks total heap usage: 10 allocs, 4 frees, 8,853 bytes allocated 40 bytes in 6 blocks are definitely lost in loss record 1 of 1 at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x40109A: main (scramble.cpp:48) LEAK SUMMARY: definitely lost: 40 bytes in 6 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 0 bytes in 0 blocks suppressed: 0 bytes in 0 blocks For counts of detected and suppressed errors, rerun with: -v ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2) 4 Last Revised: 10/20/2017

4.4 Fixing Valgrind Errors A program memleak.cpp is included with the files this week. It basically contains two different tests, both of which have bugs. Build and run it (your input in bold italics): $./compile memleak $./memleak 1 Test 1 sum is 475 $./memleak 2 Enter a word: rutabaga... Both of these tests are wrong, but you will now fix them. IMPORTANT: For all of the bugs you fix in this part of the lab, you have to keep some documentation of what bugs you find and what you change to fix them. This is mandatory because you will review with your CP/TA when you check in. We recommend that you: prominently write comments in your code indicating each line you change and why you changed it keep a separate list somewhere of the chronological order of changes (was it for memleak 1 or memleak 2?) to guide your discussion with the CP/TA 1. Run the 1 st test: $./memleak 1 This program is supposed to be adding up 7 random numbers between 0 and 99 and printing out the sum. However, now let's see if there are any hidden memory issues. Let's run valgrind: $ valgrind --tool=memcheck --leak-check=yes./memleak 1 You should see 2 errors (an Invalid Read and a block of memory that is definitely lost). An invalid read means you are trying to access a piece of memory that is not allocated to you. Usually it's because you have a bad pointer or access things beyond the end of an array. Use the line numbers in the error messages as clues and try to fix the code (Google the error message if you are having trouble understanding it). Recompile memleak.cpp and rerun valgrind until you have 0 errors. Last Revised: 10/20/2017 5

2. Run the 2 nd test: $./memleak 2 This program should ask you for a word, then print it, and print it in reverse. You may see a spew of text as the program completes. Scroll to the top and you'll see a message like: *** glibc detected ***./memleak double free or corruption This is indicative of dynamic memory problems. Let's run valgrind $ valgrind --tool=memcheck --leak-check=yes./memleak 2 You should see 3 errors. Try to read the messages (and line numbers) carefully to understand what it is saying. Some errors may be due to the same root cause. So as you fix one, re-run to see if it helps. Recompile and rerun valgrind until you have 0 errors. 4.5 Rerun Word Scramble through Valgrind Re-run your scramble.cpp program (word scramble with word bank code) through valgrind and ensure there are no errors. $ valgrind --tool=memcheck --leak-check=yes./scramble wordbank.txt Demonstrate your working code to your TA/Sherpa and change a few words in the wordbank to demonstrate the working file I/O. Also, make sure it has no memory errors or leaks by using valgrind. You should use valgrind in every lab and homework assignment from now on because it will help you check for: Memory leaks (forgetting to deallocate) and corruption Using arrays out-of-bounds Accidentally using garbage values 6 Last Revised: 10/20/2017