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

Similar documents
CS 103 Lab The Files are *In* the Computer

EE 355 Lab 4 - Party Like A Char Star

CS 103 Lab 6 - Party Like A Char Star

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

CS 103 Lab - Party Like A Char Star

CS103L SPRING 2018 UNIT 7: FILE I/O

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

EE 355 PA4 A* Is Born

Creating a String Data Type in C

CMSC 341 Lecture 2 Dynamic Memory and Pointers

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

Lecture 14 Notes. Brent Edmunds

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

Pointer Accesses to Memory and Bitwise Manipulation

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:

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.

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

A Capacity: 10 Usage: 4 Data:

Use Dynamic Analysis Tools on Linux

EE 355 Lab 3 - Algorithms & Control Structures

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

EE 109 Lab 8a Conversion Experience

Intermediate Programming, Spring 2017*

Programming Tips for CS758/858

TI2725-C, C programming lab, course

CSE 333 Midterm Exam Sample Solution 7/28/14

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

Praktische Aspekte der Informatik

CS 1044 Program 6 Summer I dimension ??????

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

ECE551 Midterm Version 1

Pointer Casts and Data Accesses

COP Programming Assignment #7

Project #1 Exceptions and Simple System Calls

CS150 Assignment 7 DNA!

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

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

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

Debugging (Part 2) 1

Scientific Programming in C IX. Debugging

Recitation 10: Malloc Lab

Homework 3 Grade Database Management Due Date

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CS 322 Operating Systems Programming Assignment 2 Using malloc and free Due: February 15, 11:30 PM

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

Homework #1 From C to Binary

CS 11 C track: lecture 5

CSE 15L Winter Midterm :) Review

EE 352 Lab 3 The Search Is On

CS 103 Unit 11. Linked Lists. Mark Redekopp

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CSE 361S Intro to Systems Software Lab #4

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

6.S096: Introduction to C/C++

MP 1: HTTP Client + Server Due: Friday, Feb 9th, 11:59pm

Should you know scanf and printf?

CSE 160 Discussion Section. Winter 2017 Week 3

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Lecture 2, September 4

Heap Off by 1 Overflow Illustrated. Eric Conrad October 2007

Laboratory Assignment #3. Extending scull, a char pseudo-device. Summary: Objectives: Tasks:

CS 103 The Social Network


ECE551 Midterm Version 2

ECE 551D Spring 2018 Midterm Exam

377 Student Guide to C++

CS61, Fall 2012 Section 2 Notes

Recitation: Cache Lab & C

Accessing Data in Memory

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

CS24 Week 3 Lecture 1

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

Laboratory Assignment #3. Extending scull, a char pseudo-device

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Project #1: Tracing, System Calls, and Processes

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

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

EECE.2160: ECE Application Programming

o Code, executable, and process o Main memory vs. virtual memory

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

CS61C : Machine Structures

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

Computer Science Foundation Exam

Programs in memory. The layout of memory is roughly:

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

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

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

Lab 4: Bash Scripting

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 27 Final Exam Revision

CS11 Intro C++ Spring 2018 Lecture 1

Recitation: C Review. TA s 20 Feb 2017

Class Information ANNOUCEMENTS

CSC 1600 Memory Layout for Unix Processes"

1. Introduction. 2. Deliverables

Programming Assignment Multi-Threading and Debugging 2

Exploring the file system. Johan Montelius HT2016

Project 0: Implementing a Hash Table

Transcription:

1 Introduction In this lab you will modify a working word scramble game that selects a word from a predefined word bank to support a user-defined word bank that is read in from a file. This is a peer evaluated lab where your grade will be determined by 2 peer evaluations. Please see the guidelines for peer evaluated labs provided by the instructor. 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) Understand deep vs. shallow copy Use valgrind to find memory leaks and other memory issues 3 Background Information and Notes Review your lecture slides in the Pointers unit paying special attention to the "Dealing with Text Strings" and "Deep vs. Shallow Copy" slides at the end of the pointers unit. Some of the code in those slides may serve as a very useful template for how to code portions of this lab. The goal of this lab will be to modify the provided word scramble code to remove the current hard-coded wordbank and allow the user to specify the filename of a text file that contains the words for the word bank. If the text file with the word bank is named 'wordbank.txt', the user would run the program passing that name as a command line argument: $./wscramble_fio 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 how the words can either be on the same line or separate lines as long as there is whitespace between them (recall how >> for cin and ifstreams deals w/ whitespace): Last Revised: 2/18/2016 1

6 ee355 trojan midterm aced perfect score wordbank.txt 4 Procedure 4.1 Word Scramble Update Download the completed (but hard-coded word bank) word scramble code. $ wget http://ee.usc.edu/~redekopp/ee355/code/wscramble_fio.tar $ tar xvf wscramble_fio.tar This will extract the following files: wscramble_fio.cpp C++ source file used for your lab wordbank.txt - Text file to be used as the word bank In this assignment our code will select a random word from the workbank, scramble the letters and ask you to guess what the original word is. It is a bit like the hangman game from the previous lab but now we're doing a word scramble. We have provided the working skeleton that does most of the game play operation, but you will modify it to handle any wordbank. Make the following alterations to wscramble_fio.cpp: 1. Include the appropriate file stream header file 2. Delete the global wordbank array declaration and numwords declaration 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. 2 Last Revised: 2/18/2016

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 [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 (reference the code give in our slides about "Dealing with Text Strings" in the Pointer Unit Slides 53-57) 9. Close your input file At this point the original word scramble code should be able to be reused (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 (remember to pass the wordbank.txt file as a command line argument to your program when you run it). Now learn about a tool named Valgrind because you will need to double-check your word scramble program using Valgrind before you submit your lab. 4.2 Introduction to Valgrind Valgrind: Just like 'gdb' can be used to help us find runtime errors in our program, 'valgrind' is helpful tool to track down memory allocation and usage errors. 'valgrind' will run your program inside of itself (just like 'gdb') and tell you if you misuse memory or forget to deallocate it. You run the program using the name 'valgrind' followed by several options to tell valgrind what you want to do (i.e. --tool=memcheck --leak-check=yes ). You then provide your program name and any command line arguments just as you would if you were running it normally at the command prompt. So it should look like this: $ valgrind --tool=memcheck --leak-check=yes./wscramble_fio wordbank.txt 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 number should match. If they don't you leaked memory. ==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 Last Revised: 2/18/2016 3

==8146== ==8146== For counts of detected and suppressed errors, rerun with: - v ==8146== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) For example, if you forget to deallocate the words from the wordbank, you might see an error summary like this. Here I allocated 10 blocks but only freed 4. Uh-oh, 6 blocks were not freed. The great thing is it shows you which line of code [provided you compiled with the g debugging flag in g++] allocated the memory that was never freed (look for the bold type). 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 (wscramble_fio_sol.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) Let s play with Valgrind with a real example. Download the sample program. $ wget http://ee.usc.edu/~redekopp/cs103/memleak.cpp Now open 'memleak.cpp' and examine the code but don't change anything. Compile the program. $ g++ -g o memleak memleak.cpp There are two tests that can be run by passing in an appropriate command line argument: or $./memleak 1 $./memleak 2 4 Last Revised: 2/18/2016

1. Run the 1 st test: $./memleak 1 You should see some random number printed which is the sum of the random array. But notice the program runs successfully. 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. 2. Run the 2 nd test: $./memleak 2 You may see a spew of text as the program completes. Scroll to the top and see that the first two lines should look like the word you were asked to enter and then that word reversed (maybe). But then 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.3 Rerun your WordScramble through Valgrind Re-run your wscramble_fio program (word scramble with word bank code) through valgrind and ensure there are no errors. $ valgrind --tool=memcheck --leak-check=yes./wscramble_fio wordbank.txt Last Revised: 2/18/2016 5

5 Evaluation Criteria 1. [3 pt.] Run the word scramble program using the provided/default wordbank file and guess the word correctly. Verify the program runs correctly. 2. [3 pt.] Run the word scramble program using your own wordbank file with only 2 words (you make them up). Verify the program runs correctly. 3. [1 pt.] Run the word scramble program but provide a filename that DOES NOT exist as a command line argument Make sure the program exits immediately. 4. [1 pt.] Run the word scramble program but provide a wordbank file that does not have a number (number of words) as the first item in the file. Run the program passing in that filename and make sure the program exits immediately and does not crash. 5. [1 pt.] Run valgrind on the program using the default wordbank and ensure there are no errors and no memory leaks (i.e. unallocated memory). $ valgrind --tool=memcheck --leak-check=yes./wscramble_fio wordbank.txt 6. [1 pt.] Verify the program is indented when opened in gedit on the Linux VM. 6 Last Revised: 2/18/2016