🔬 Lab
CS 240 Commands
Preparatory Questions
Commands Assignment Prep Questions
These questions will ensure that you’ve read and understood the instructions for the commands assignment, and cover a few edge cases that you’ll have to be aware of as well as some core concepts that you’ll need when designing your solution.
Consult the assignment instructions in order to answer these questions.
Structure of a Command
Can spaces appear at the start or end of command lines?
Yes
No
Is the & character ever considered part of a command
word?
Yes
No
Can a command line with zero &s be valid?
Yes
No
Can a command line with two or more &s be valid?
Yes
No
Is a command line that consists of just zero or more spaces valid?
Yes
No
Is the command line consisting of just & valid?
Yes
No
How to Check Format Rules?
You can answer questions like those in the previous section by adding new test commands to which file?
Correct answer: command_lines.c
…and then invoking which command to show the expected outputs for each test command line?
Correct answer: make test-expected
The Status Result
For a valid command with zero ampersands, what should be stored in
status? Correct
answer: 1
For a valid command with one ampersand, what should be stored in
status? Correct
answer: 0
For any invalid command, should status be modified at
all?
Yes
No
Idiomatic Pointer Style
To write code in idiomatic pointer style, is it sufficient to replace
all occurances of the array subscript notation array[index]
by the pointer notation *(array + index)?
Yes
No
Strings and Lengths
How many bytes should be malloced to store the string
"hello", assuming each character is stored in 1 byte?
Correct answer: 6 Explanation: Remember each string must include a NUL at the end.
If a command line has n words, what is the formula for the
number of bytes that should be malloced to store the array
of pointers to these words (ignoring the bytes required for each word
itself)?
8 *
n
4 * (n +
1)
8 * n +
1
8 * n +
4
8 *
(n + 1)
Hint: Each pointer requires 8 bytes and we are using NULL
to mark the end of the array.
Including both the bytes used to store individual words and the bytes
required to store the NULL-terminated array of pointers to words, how
many bytes would need to be allocated via malloc to store
the command array consisting of the three words
"ls" "-l" "cs240"?
How many bytes are needed to store just the three words?
Correct answer: 12
How many bytes are needed to store the NULL-terminated array of pointers?
Correct answer: 32
If we call malloc once for the pointers array and then
once for each word, how many calls to malloc will be
needed?
Correct answer: 4
Is command_parse allowed to mutate the contents of a
command line string it receives as an argument?
Yes
No
Tools
What tool is used to check for memory safety violations?
Correct answer: valgrind
What tool should you use to debug your code if it isn’t working?
Correct answer: gdb
In the debugger, what command would be used to set a breakpoint?
Correct answer: break
In the debugger, what command would be used to see details about the different options for setting a breakpoint?
Correct answer: help break
Which make commands can be used to test your
command_parse function?
make test-expected
make test-show
make test-free
make test-parse
make test-demo
make test-adversarial
