Purdue CS24000 2022 Summer Final Exam Solutions
Purdue University CS24000 is an undergraduate-level course that teaches students programming principles and techniques for problem-solving in the C programming language. Here are the solutions and study notes for the 2022 and 2023 Final exams.
Introduction
Below are extracted from the Summer 2023 CS24000 course homepage:
Disclosure: This blog site is reader-supported. When you buy through the affiliate links below, as an Amazon Associate, I earn a tiny commission from qualifying purchases. Thank you.
- Course Title: Programming in C
- Textbook: The C Programming Language (2nd Edition); Kernighan and Ritchie; March 1988
- Reference: Beej’s Guide to C Programming; Brian “Beej” Hall; 2007
Summer 2022 Final Solutions and Notes
Problem 1 (45 pts)
(a) Which statements in the code
1 | typedef struct friend { |
are problematic, likely to trigger segmentation fault? Augment the code by adding calls to malloc()
so that the bugs are fixed.
(b) Explain the difference between fun1 and fun2 which are declared as char *fun1(char *)
and char (*fun2)(char *)
, respectively. Code a function fun3 that takes a string as argument and returns the last character of the string. You may assume that the string is of length at least 1 (not counting EOS).
(c) Suppose a user enters the command, %/bin/cp file1 file2
, using a shell to copy the content of file1 to file2 on one of our lab machines. From the viewpoint of the shell, from where does it read its input /bin/cp file1 file2
? From the viewpoint of the app /bin/cp
which is coded in C, how does it access its input which specify the names of two files whose content is to be copied? Before calling execv()
what must the shell do to prepare the arguments of execv()
so that /bin/cp
has access to the two file names?
Problem 1 Solution
(a) Problematic:
1 | amigo->year = 2017; |
The reason is that the pointer amigo
has not been initialized to the address of any allocated memory space yet.
Agumentation:
1 | amigo = (friend_t *)malloc(sizeof(friend_t)); |
(b) fun1 takes as argument a pointer to char and returns a pointer to char. fun2 is a function pointer to a function that takes as argument a pointer to char and returns a value of type char.
1 | char fun3(char *s) { |
(c) Input /bin/cp file1 file2
is read from stdin.
main(int argc, char *argv)
of /bin/cp
accesses the two file names via argv[1]
and argv[2]
.
Assuming a variable s
is of type, char **s
, a shell must allocate sufficient memory for s
and copy /bin/cp
into s[0]1
, file1
into s[1]
, file2
into s[2]
, and set s[3]
to NULL.
Problem 2 (30 pts)
(a) Code a function, unsigned int countdbl(long)
, that takes a number of type long
as input, counts the number of 0s in the bit representation of the input, and returns 0 if the count is an even number, 1 if odd. Use bit processing techniques to solve the problem. (b) gcc on our lab machine, by default, will insert code to detect stack smashing at run-time. What does gcc's code try to prevent from happening? In the case of reading input from stdin
(or file), what is a common scenario and programming mistake that can lead to stack smashing? Provide an example using scanf()
(or fscanf()
). What issound programming practice that prevents stack smashing?
Problem 2 Solution
(a)
1 | unsigned int countdbl(long x) { |
(b) When a function is called by another function, gcc tries to detect if the return address has been corrupted and, if so, terminate the running program.This is to prevent the code from jumping to unintended code such as malware.A local variable of a function declared as a 1-D array overflows by input whose length is not checked when reading from stdin (or file).Example: a function contains code
1 | char buf[100]; |
which may overflow buf[]
since scanf()
does not check for length of the input.Sound practice: use functions to read from stdin (or file) that check for length.In the above example use fgets()
instead of scanf()
.
Problem 3 (25 pts)
Code a function that takes variable number of arguments, double multnums(char *, ...)
, multiplies them and returns the result as a value of type double
. The fixed argument is a string that specifies how many arguments follow and their type (integer 'd' or float 'f'). For example, in the call multnums("dffd", 3, 88.2, -100.5, 44)
, the format string "dffd" specifies that four arguments follow where the first character 'd' means the first argument in the variable argument list is of type integer, the second and third 'f' of type float, and the fourth 'd' of type integer. Forgo checking for errors and ignore header files. What would happen in your code if multnums
is called as multnums("dffd", 3, 88.2, -100.5, 44, -92, 65)
? What about multnums("dffd", 3, 88.2, -100.5)?
Explain your reasoning.
Problem 3 Solution
1 | double multnums(char *a, ...) { |
When a C function is defined with a variable number of arguments, it typically uses the va_arg
, va_start
, and va_end
macros from the <stdarg.h>
header to handle the variable arguments.
If the input argument count does not match the format string provided to functions like printf
or scanf
, it can lead to undefined behavior and potentially cause crashes, memory corruption, or incorrect output/input.
Here are some specific scenarios that can occur when there is a mismatch between the input arguments and the format string:
- Too few arguments:
- If there are fewer arguments than the number of format specifiers in the format string, the behavior is undefined.
- The function may attempt to read from uninitialized memory locations or use garbage values, leading to incorrect results or crashes.
- Too many arguments:
- If there are more arguments than the number of format specifiers in the format string, the extra arguments will be ignored by the function.
- However, if the extra arguments are of a different type than expected, it can lead to incorrect interpretation of the data on the stack, potentially causing crashes or memory corruption.
Bonus Problem (10 pts)
Suppose an ASCII file contains lines where each line is a sequence of characters ending with \n
but for the last line which ends because the end of file is reached. The goal of main() is to read and store the lines of the ASCII into a variable, char **x
, where malloc()
is used to allocate just enough memory to store the content of the file. Using only basic file I/O operations discussed in class, describe in words how your code would work to accomplish this task. Be detailed in how the arguments of malloc()
are determined to store the file content in x
.
Bonus Problem Solution
- Open file, read byte by byte until EOF is reached while counting occurrences of '' to determine the total number of lines (count plus 1). Denote this number of r.Close file.
- Use
malloc()
to allocate 1-D array,int *M
, of size r of type int. Open file, read byte by byte, counting for each line the number of bytes. Store the line lengthin 1-D array M. Close file. - Using 1-D array M call
malloc()
for each line to allocate memory to store the bytes of each line. Point x to the 1-D array of pointers to char. - Open file. Read byte by byte the content of each line into 1-D array of pointersto char pointed to by x.
A sample implementation (not required for this exam) is shown as below:
1 |
|