Assignment questions and answers

Here are some questions and answers from earlier assignments which apply just as much to assignment three.

No new entries will be added to this list after its publication date for assignment three -- new questions-and-answers, even if they apply to CSC 270 assignments in general, will go in the assignment three Q&A file.


Q: I had some last-minute problems last night [November 21st], so I'm going to hand it in today, ok?
   or

The CDF computers were busy last night, ...

A: No, late assignments are not ordinarily accepted. None of the above is an unforeseen circumstance. If you are leaving your work for the last minute, please assume that some other students will too. We strongly recommend that you not leave things for the last minute.

At this date [November 22nd] it's a bit late for this answer to be of use to you for assignment three. Fortunately I have a time-warp here so I'll post this on the CSC 270 web pages retroactively on November 5th. Please be advised.


Q: How do I compile multiple files together into one program?

A: List multiple files on the command line. For example,

	g++ -Wall -ansi -pedantic main.cpp event.cpp applications.cpp -lm
Or in fact you may be able to use *.cpp, so long as there are no .cpp files in the directory which shouldn't be part of the compilation.


Q: Do I have to limit the length of the lines to 80 characters, as I did in CSC 148?

A: Yes. The printer, my computer monitor, etc, are all of finite width. The standard terminal width is 80 characters; all you can expect from me in terms of line width, e.g. in how I format the print-outs, is that I make 80 characters or fewer look acceptably. If lines are longer than that, they will wrap around. Instead, you should wrap them around yourself, taking care to make the result tidy and readable.

Q: How about the report file? If I limit the lines to 80 characters, I can't say as much as I want to.

A: If there is no limit on the lines' width, then the restriction of 60 lines is not really much of a restriction. Yes, this restriction means you are limited to 80x60 characters. We are not seeking an extensive analysis; just make a conclusion about the different delay strategies, with some reference to sample results.


Q: My program works perfectly but when compiled, there are error or warning messages. Can I leave it like this?

A: No. (Well, when the due time approaches, you should submit whatever you've got; but if you have time, you should correct these errors.)

Warning messages indicate that you are doing something improper. It may fail to work on other C compilers, for example (whether this is what's going on depends on the particular error message). In this course we are writing careful, correct, tidy, and maintainable C programs.

Last year, ajr wrote a newsgroup article about this, which the above paragraph is from.


Q: I changed the statement

        x += y;
to
        if (y == 1)
            x++;
        else
            x += y;
, but this is harder to read. Which is more important, efficiency or "style"?

A: You are mistaken about efficiency. The second version will likely run slower than the first, and it will produce a larger compiled program, and it is more likely to contain errors (bugs), and future modification to this program is more likely to introduce bugs because of its unnecessarily complex structure. The most important thing, both in this course and in "real life", is to write your program clearly and correctly.

Many times when people modify their programs in this sort of way for "efficiency" they cause harm and no good. That is, the best you can hope for is that the program works almost as well as before.

There's a saying: "The first rule of optimization is: Don't."

You will have noticed that many large software projects produce programs which are abominably slow. The solution, though, is not to perform "micro-optimizations" of this kind; they may or may not help, but any help will be imperceptible and won't fix the situation. The main way one speeds up unacceptably-slow programs to an acceptable speed is to improve the algorithms. This is much harder when "micro-optimizations" are in place.

Occasionally one does perform these sorts of micro-optimizations, when you're trying to speed up a program in every last possible way. But you do them last, and you only micro-optimize the sections of the code which profiling data has shown to be worth the cost. (Usually this is no code at all; i.e. you don't do it.) The cost is substantial: it creates difficulty reading and working with the program. The benefit of most micro-optimizations is basically zero. Zero benefit for substantial cost is not a good tradeoff.


Q: How do I check whether or not my files have been submitted properly?

A: Type the following command:

submit -l -c csc270h -a a3
That "-l" is the letter L (but lower-case), not the number 1. It is like the "-l" in "ls -l".

This command does not give you the file contents, but it gives you the file size, so you can compare to an "ls -l" of that file and verify whether or not it's the correct one, and you can see whether all your files have been submitted.

You can resubmit a file by simply submitting a file by the same name again. The last one counts.

For assignment three, you can cancel a file (ask us to disregard it) by submitting a zero-sized file of the same name. The "submit -l" command will also allow you to see whether it is zero-sized.


Q: How do I find out about library functions?

A: Every library function has a unix "man page" ("man" is short for "manual"). You can read the exp man page by typing "man exp".

A2: Look it up in the index of your C book, if you have one. Your C book might have a list of commonly-called functions (which would be more useful than a list of absolutely all library functions).


Q: I get some errors when compiling with -ansi and/or -pedantic, but not when compiling only with -Wall. Do I have to fix them?

A: Yes, you have to fix them. Your program should compile with no errors or warnings when compiled with "gcc -Wall -ansi -pedantic".

(Also see the q&a above about warning messages, and the above-cited newsgroup article.)


Q: What do compiler warnings about "carriage return" mean?

A: They mean that there are spurious control-Ms in your file. Control-M in the ASCII standard is called "carriage return".

These control characters probably arose because you transferred your file incorrectly from ms-dos to unix. The ASCII standard does not specify how lines of a text file are separated. In ms-dos, the line terminator is the two characters control-M and control-J (in that order). In unix, the line terminator is the single character control-J. When you transfer text files between computers, the file transfer program should have an option to change the line terminator character appropriately.

If you transferred your file in the wrong mode, you can fix it up with the "flip" command, as described in the Student Guide to CDF, page 13 ("Removing line feeds").


Q: The examples in the King book say "main()" instead of "int main()". Which one is right? And why do both of them seem to work?

A: There's a rule in C which is mostly historical and is quite silly, which says that the default type for things is int. In particular, the code

    int gloop()
    {
	return 3;
    }
is exactly the same, by definition, as
    gloop()
    {
	return 3;
    }

Almost everyone agrees that the latter form is bad. However, some people make an exception for main() for historical reasons. I think that this exception is pointless but harmless. However, leaving off an "int" in any function declaration other than main() is certainly grounds for a complaint by the grader. And since gcc -Wall -ansi -pedantic will indeed give you an error (warning) message for leaving off the "int" in the declaration of main(), you should put the "int" in there too.

The fact that you are/were allowed to leave out the word "int" in declaring a function to have return type int has led to two quite separate uses of this laxness. For one, some C programmers used to omit the type declaration to indicate that no value was returned, especially in the very early days before the "void" type was added to the language. However, other C programmers got used to leaving off the "int" just because you could.

This means that other than in the case of main(), you really don't know what is meant by a missing type declaration these days. If you mean that the function returns int, say "int"; if you mean that the function doesn't return anything, say "void". This is why the modern version of gcc gives a warning if "return type defaults to 'int'".


Q: Can we assume a maximum number of hosts to so as to be able to declare an array? Can I put a #define MAXHOSTS 100 somewhere?

A: No. If your program is run with "./a.out 200 200 200 1", it should work unless it doesn't have enough memory. Your program must allocate space dynamically to at least this extent.

Use C++'s "new" feature to allocate runtime-sized arrays.


Q: My program gives a "segmentation error" (or any other kind of run-time OS error), but I can't figure out where it is happening. What's the best way to track these down?

A: For most investigation into program behaviour, simple printfs scattered through the code, telling you the value of variables at interesting points, gives a lot of information. But specifically for mysterious crashes, you can compile and link with -g, and then use gdb, a debugger. I can't really tell you a lot here about using a debugger, but I wrote out a short example session which might help. The key command is "where" (then "quit").


Q: Help! My program doesn't work!

A:

  1. Think of a particular undesirable behaviour your program exhibits. ("Doesn't work" isn't a good enough starting point for a debugging task.)
  2. Trace your program up to the point of misbehaviour and figure out what the values of variables should be at the various points.
  3. Put in printf statements which show the values of variables at various points so that you can determine at what point the behaviour of your program is diverging from your expectations.
To determine the point of misbehaviour when the evidence of misbehaviour is a run-time exception such as "segmentation exception" or "bus error", use gdb as discussed in the previous question.


Q: How do I initialize members of a class? The following gives a compile error:

class symbol {
    ...
    int somethingelse = 3;
    ...
};

A: That is because the class definition defines a type, not an object. There isn't an actual "somethingelse" member to initialize, not yet, until you declare or malloc one of these class.

So you have to assign to it in the constructor.


[
assignment three q&a page] [main course page]