



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Goal: The purpose of this assignment is to learn to use dynamically allocated memory and structures.
Objectives: be able to write code that reads data from files (fopen(), fclose(), fgets()); be able to write code that uses malloc() and realloc() to dynamically allocate memory; be able to write code to implement a dynamic array list; be able to write code to use/manpiluate an array of structures; be able to write code to identify whether two strings match.
Write a program phonebook1 that will read in a file containing pairs of lines names and phone numbers, place those into a dynamic array list, and then read in a list of names (one per line) from the standard input. For each name read from the standard input, the program should determine if there is an exact match in the dynamic array list. If there is a match it should print out the name, followed by the phone number. If there is not a match, it should print out the name followed by the string "No match found.".
The program should work with input and produce output exactly as described here. (Automated tools that do a character by character comparison may be used for grading.) For inputs that do not comply with this specification, your program may print an error message and exit; there should not be any inputs which would cause your program to have undefined behavior.
phonebook1 command line. The program executable should expect as a command-line argument the name of a file containing a list of phone numbers. Example:
./ phonebook1 phonelist. txt
data file. The file specified on the command-line (e.g., phonelist.txt will contain lines of text of up to 80 non-null characters. (If there are more than 80 -charactersānot counting '\n'āon a line, the additional characters should be silently discarded, producing the same result as if all of the lines were truncated to 80 characters.)
Each pair of lines will represent one āphonebook entryā. The text of odd lines will be considered to be a personās name. The text of even lines will be considered to be the phone number of the person named on the previous line. The program should treat these items as uninterpreted strings of up to 80 non-null characters. (In particular the program should not not try to parse the
name to determine the last name or parse the the phone number to encode the individual digits.) The entries will not necessarily be sorted in any way.
An example data file is:
John Smith +49 1234 5 - Jeff 458 - Abe Lincoln 1 -888 - WIT - HOUS
Standard Input. The program should interpret each line on the standard input as a name that it should attempt to find in the list. (If there are more than 80 -characters + '\n' on a line, the additonal characters should be discarded.) The program should stop when it sees a blank input line or EOF.
An example of some input to the program would be:
Jeff Dr. von Ronne Supe r c a l i f ragilisticexpialidoc i ous John Smith Abe Jeffery
Bubba
For testing, you may wish to redirect the input from files containing lists of names such as that found in this example.
Standard Output. For each of the lines on the standard input, the program should attempt to find a phonebook entry whoās name is an exact match of the name in the standard input line. If there is a match, the program should output the name, a colon and space, and then the phone number on a single line. If there is no match, it should output the name, a colon, and the text, "No match found.".
The output for the example inputs listed above would be:
Jeff : 458 - Dr. von Ronne : No match found. Supe r c a l i f ragilisticexpialidoc i ous : No match found. John Smith : +49 1234 5 - Abe : No match found. Jeffery : No match found.
Your main program should be made up of the files: strmatch.h, strmatch.c, phonebook.h, phonebook.c, pbe-darray.h, pbe-darray.c, and phonebook1.c.
structure and print out all of the pbentryās in the dynamic array list. The main function should initialize a pbe_darray, append several pbentry structures (with dynamically allocated name and number strings), print them out using print_phonebook(), and free the memory used by the dy- namic array list. The pbe_darray should be initialized with an initial capacity of small enough number to make sure that it gets to be enlarged at least once as the main function appends entries.
Create a Makefile with rules to compile phonebook.c and test-darray.c and link the object files together into the executable Run test-darray to unit test your code. Be sure to run test-darray under valgrind to increase the chances of detecting memory allocation/pointer problems that cause undefined bheavior.
read_phonelist(). Implement a function read_phonelist() to read in pairs of names and phone (formatted as described above) from a file stream and append them to a dynamic array list of phone book entires. This function should be declared in phonebook.h and defined in phonebook.c.
The function read_phonelist() should take a FILE * parameter, which specifies the file stream from which it should read, and a pointer to a pointer to a pbe_darray structure, in which it will store the data read from the file. Each line from that file should be copied into a dynamically allocated string just large enough to hold that line (include '\0') without any wasted space. The strings from each pair of lines should be put into pbentry structure appended to the pbe_array.
Create a sample input data file, and extend the main function in test-phonebook.c to ex- ercise read_phonelist() on that file, printing out the result. Modify the Makefile to be able to link test-phonebook.o with phonebook.o and darray.o to produce test-phonebook. Run test-phonebook to unit test your code.
pbe_lookup(). Implement a lookup() function that takes a string pointer to a name and a pointer to the pbe_darray and finds the first pbentry in the pbe_darray with a matching name. If a match is found, it should return a pointer to that pbentry, otherwise it should return the NULL pointer.
The pbe_lookup() function should use the the strmatch() function and pbe-darray.c functions to perform its tasks. The definition should be in phonebook.c and declaration in phonebook.h.
Extend the main function in test-phonebook.c to also exercise a successful and unsuccessful search with pbe_lookup(). Run test-phonebook to test your new code.
main(). Write a main function in the file phonebook1.c that will use the functions described above to carry out the programs tasks. Specifically, it should read in the file specified on the command-line in, pulling out the ānameā and ānumberā strings from the file (as described above), dynamically allocating space for those strings, and placing them into a dynamic array list of pbentry structures. (To make testing easier, use an initial dynamic array list capacity of 5 .) The main function should, then, read in a list of names (one per line) from the standard input. For each name read from the standard input, the program should determine if there is an exact match in the dynamic array list. If there is a match it should print out the name, followed by the phone number. If there is not a match, it should print out the name followed by the string "No match found.".
Add a Makefile rule to compile phonebook1.c and a rule to link strmatch.o, phonebook.o, pbe-darray.o, and phonebook1.o into an executable called phonebook1.
Test your program thoroughly using valgrind and a variety of valid and invalid inputs.
In implementing this assignment, there are lots of opportunities to introduce bugs that only show up only rarely. In general, some of the reasons for this can be ( 1 ) that the code branch containing the bug may only be executed on certain inputs, ( 2 ) that the error may only be observable for certain inputs (often related to āboundary conditions,ā such as the size of the input related to the size of internal buffers), or ( 3 ) that the error may result in undefined/nondeterministic behavior which happens to be what the programmer expects most of the time. Problems ( 1 ) and ( 2 ) become more problematic as programs get more complex. A thorough regimine of unit testing is helpful in addressing these problems at least for faults that are entirely contained in a single code unit. Problem ( 3 ) can result from memory allocation / pointer bugs, and so may be more of an issue with the programs you write in this class than with programs you have written previously.
Here are some techniques/tools for help you find faults in your program:
testing boundary conditions Software failures are often the result of getting a test āoff-by-oneā (e.g., using < when <= was meant). For this reason it is often useful to test programs just on both sides of the āboundariesā where the programs behavior should change. (These bound- aries can be found by looking at the specification of what the code should do. A related technique is to look at the if statements in the code and make sure, youāve tested inputs which execute both the āthenā and the āelseā parts of as many if statements as possible.) For example, in this assignment, lines longer than 80 characters should be treated as if they were truncated to 80 characters, so it would make sense to test the program with lines, which are 79 characters, 80 characters, and 81 characters along with lines that are much shorter or much longer. unit testing As software systems get bigger and more complex, the possible execution paths grows super-linearly and it becomes infeasible to test all of the combinations. Well-designed software is divided into modules that have a well-defined interface (this is one of the pri- mary purposes of classes in OO languages). Testing that each module obeys its interface is usually more tractable than testing the whole program. Testing of a single module (or unit) is called unit testing and can be more effective at finding the bugs within that unit than whole program testing. (On the other hand, another source of bugs is the complex interac- tions between modules, and to find those you need to also test the whole program (systems testing), subsystems within the program, or groups of modules together (integration test- ing). The ātestā programs you implemented above are examples of unit and integration testing. MALLOC CHECK The GNU libc memory allocation functions understand the environment variable MALLOC_CHECK_. If this is set to 3 , malloc() and friends will do some extra checks each time they are run (e.g., make sure you donāt free the same pointer twice) and aborts the program execution with an error message when such checks fails. This slows the program down, but helps you catch and fix more bugs. If you are using the tcsh shell (which you will on the department machines), you can set the environment variable with the command:
(b) Did you work with anyone or get help in completing this assignment from anybody other than the instructor or TA? (This includes mentors and tutors.) If so, please de- scribe the nature and extent of the help received. If such help or collaboration exceeds what is allowed for this assignment according to these directions, points may be deducted, but as long as the help or collaboration is completely disclosed in the README file, it will not be considered to constitute scholastic dishonesty. Below your answer to those two questions, include the following statement: I certify that except as noted above, the work I submit for this assignment has been completed solely by me without any outside help and without looking at any code for solving this problem other than what is contained in this document or included in other material/links provided by the instructor or TA.
Below this statement, please write your name and date.
Use svn to commit your final submission by 5 pm on the due date.
Permissible Collaboration and Resources. This assignment is to be completed individually. You are allowed to discuss general strategies for solving assignments with fellow students or other in- dividuals, but it is no longer a āgeneral strategyā if the discussion gets to the level of detail of what would be done in actual lines of code found in your programs. In addition, discussions of āgeneral strategyā should not be taking place while either party is editing their source code. (It is, however, perfectly acceptable to discuss C language constructs and concrete examples of them which are not taken directly from anyoneās solution to an assignment.) In addition, you may seek more specific help from mentors and lab tutors in trying to understand why their code doesnāt work. Furthermore, you may also seek help and guidance of any kind from the TA and instructor.