General comments regarding MP1
These are some general observations that many of you might already be aware
of, but few students still need to know...
1. about makefile...
-
your dependency list should include all the files that are used in
the module. for example...
# suppose your target is "command.o" and you have...
commands.o: commands.cpp
g++ -c commands.cpp
# now, suppose "commands.cpp" includes and uses declarations from "commands.h"
& "global.h". but according to
# the dependency list mentioned above, make utility doesn't know about
this "dependence", hence any modifications
# to "command.h" or "global.h" won't result in recompilation of "command.o",
something undesirable (and possible
# cause of linker/runtime errors ;o). Hence all ".h" files (that are
included in ".cpp" file) should be added to the
# dependency list of that module.
-
your dependency list should NOT include files that are not used in the
module...
# now, this is also important. if you are not including some files in "commands.cpp",
then they should not appear in
# the dependency list of this module. otherwise it may lead to unnecessary
recompilations, which at the very least
# results in waste of time !!!
2. about ".h" & ".cpp"
-
It is very important to know the difference between "declaration" and "definition"
!!!
-
By convention, and this may be of interest to those switching from
Java to C/C++ (although its rare), ".h" files are used for declarations
only. all the definitions should be in the ".cpp" files.
-
this also implies that global variables should be "defined" in ".cpp" files,
where as they can be "declared" as "external variables" in some ".h" file
that is included in other modules that use these variables.
-
lastly, avoid using global variables at all. instead, a better approach
would be to have an "environment" class or structure, which can be initialized
and passed as parameter among methods/functions that need access to system
environment.
3. if (argv[i]=="-o")...
You should be aware what the above statement means !!!
well, for those who still aren't sure why this doesn't work "as expected",
here is some "new" info...
C (and C++) treats all strings as constant character pointers. now,
a pointer by itself means nothing but some address in the program. since
"argv" (the second argument from main(...)) is a "char **" and "-o" is
a string, above statement is actually comparing memory address, where
i'th command line argument is stored, to the address of memory location
where "-o" string is stored. this essentially is different from comparing
the two strings. hence, you should be using "strcmp(...)" or some variant
here.
a note: you could be having a "String" class with overloaded "==" operator
for general use. but "argv" will always have to use strcmp for historical
reasons :o)
4. about buffer...
Since this project required (most of you) to use a buffer for reading
input and scanning, my last point is to mention an elegant technique
of using a buffer which is very useful in such cases. this technique is
from "Compiler Design: by Ullman, et.al.". you can implement this
as a class (or template) for general use.
-
let maxLen be the maximum length of token that you have to scan, in your
case it was page width (why ???).
-
then have your buffer to be of size 2*maxLen, and initially read maxLen
characters from file (into the first half).
-
now, start processing the current half. when you exhaust the current half
read the other half (while not over writing the current half), and continue
scanning for the current token.
-
you see, how simple it makes the cases when you need to backtrack !!!
As the course name suggests, i hope by the end of this semester
everybody (including me :o) should have enhanced their programming skills
!!!
-amit.