[cs631apue] midterm notes

Jan Schaumann jschauma at stevens.edu
Sat Oct 23 15:27:56 EDT 2021


Hello,

I will be sending out grades for the midterm
assignment later today.  A few general notes that
apply to several of you are below:

1) Just because you find code on the internet does not
mean that you are free to use it.

The assignment asks you to write _all_ code yourself.
I understand that it's easy and tempting to google a
specific problem (e.g., "length of a number in C"),
then grab the first answer of the Stackoverflow post
that pops up, or to come across a useful function
implementation on GitHub somewhere, but you cannot
simply take such code without attribution or possibly
at all.

If the code in question is negligible in complexity,
then attribution is the bare minimum required; if the
code in question is non-trivial, then you have to
determine the copyright.  This holds for your
assignments just as it holds for private projects,
open source projects, or professional work you do.

I know it seems silly to you for simple assignments,
but as always, the assignments in school are there for
you to learn what is right and to build habits.


2) Errors should be printed to stderr, not stdout.
Error messages should be useful and descriptive, and
by convention include the program name so that it's
easy for the user to determine which command failed
when executing e.g., a pipeline.

Use

(void)fprintf(stderr, "%s: could not open '%s': %s\n",
                        getprogname(), file, strerror(errno));

or

err(EXIT_FAILURE, "name of function that failed");


3) Just because a function call succeeded once doesn't
mean it will succeed immediately afterwards.

That is, if you call stat(2) and check its return
code, then make an identical call again afterwards,
you have _no_ guarantee that the second call will
succeed just because the first one did.

You need to _always_ check the return value of any
function call that might fail.


4) Don't leave in place dead or commented out code.
Your code is not random work in progress, but a
complete, finished project.  Leaving commented out or
dead code in place makes your code hard to read and
debug.


5) If you end up passing a large number of arguments
to a function, consider using a struct or even global
variables to improve readability.

Instead of

int func(int flag, int arg1, int arg2, int arg3, char *arg4,
         char *arg5, double arg6, char **arg7, char arg8);

consider

int func(int flag, struct stuff);


6) Please don't write to any buffers that you did not
allocate yourself, such as e.g., the elements in argv.
You have no idea what size they are, so you cannot
append data to them, not even a single char.


More notes specific to each of your particular
submission will be sent out momentarily.

-Jan


More information about the cs631apue mailing list