Lab 1
Valgrind - checks memory leaks, orphaned memory, out of bounds loops (ex. invalid read of size 4)
$ valgrind ./my-program-exec --any-flag --to --program
Valgrind Plugins
- callgrind - program speed
- massif - memory usage
- helgrind - data races
Use Valgrind before submitting to the autograder
- Undefined behavior produces incorrect output
GDB - go line by line to check code (similar to XCode)
Input and Output
C++ stdin (reading in from text and outputting to text)
$ ./path281 < input.txt > output.txt
// "<" and "input.txt" are not command line args and don't appear in char*argv[]
// for XCode, ioredirect and flags can be written in scheme (double check help files in lab01)
Use getline to read whole variable, example
while (cin >> new_value) {
// reading word by word
}
while (getline(cin, new_line)) {
// reading line by line
}
// cin is ok for now, don't need stringstreams for cin, but needed for cout
Printing line by line with "endl" is inefficient due to flushing the buffer?, use" '\n' "instead
Internal Buffers (stringstream?) to improve efficiency
#include <sstream>
int main(int argc, char *argv[]) {
ostringstream ss;
for (...) // loop 100 times
ss << i << '\n'; // oss called 100 times
cout << ss.str(); // cout only called once after look, ok
}
Cerr - doesn't print anything on autograder, use it liberally, but then change it?
cerr: write to stderr
When submitting:
make partialsubmit - doesn't include .txt dictionary files
- Use this if not sure if it will compile (won't charge submit if it doesn't compile)
make fullsubmit - includes every file, includes .txt dictionary files
- Do this only when you're sure submission will compile (always charge 1 submit)
Unit testing - test stuff as you go, doesn't have to be formal, can be just a few lines in main
Regressing testing - running all unit tests to make sure code doesn't break
Getopt
struct longopts[]
while loop with switch cases
- usually several parameters in main
use the getopt_long___example file as a template
Conditionals are used to make sure the command line arguments make logical sense