EBoard 01: Getting Started
This class will be recorded! Its use will be limited to members
of the class. Please do not share with others.
Approximate overview
- Lots of administrative stuff, including attendance
- Some course background
- Approximate learning goals
- Sample C code
Administrative stuff
General Notes
- Happy foolish April day, or first day of baseball, or whatever
you celebrate.
- Hi, I’m Sam (or SamR).
- There are no class mentors
- The class Web site is (or will be) at
https://rebelsky.cs.grinnell.edu/Courses/CSC282/2021SpT2/.
- Sam: Don’t forget to put that in the chat.
- The class Web site is (always) a work in progress.
- We are using Microsoft Teams for this class. You can (should?) use Teams
for other communication, too.
- I’d like to see your face during class (as much as Teams allows),
but do not require it.
- The ellipses button (three dots) at the top lets you set a background.
- I do not pay attention to the chat during class, but may read it at
some point. (You can use it for snarky comments about my teaching,
greetings to each other, comments about pets that wander onscreen,
etc.)
- Feel free to raise your hand using the Teams “raise hand” feature.
I probably won’t notice, but others will and will tell me.
- I will forget to share my screen. Please remind me.
- I type our online class notes in a format called “markdown”. You should
find it relatively readable. It permits me to make nice Web pages.
- Sam: Don’t forget to show off today’s eboard.
- NAME! Stop making fun of Sam!
- Don’t just rely on mine; evidence suggests taking your own notes
helps you learn.
- Advance notice: I will not be here next Thursday, so there will be no
class.
- That’s okay, the College cleverly put 36 days in Spring Term 2
so that we can still get 14 class days.
Upcoming Activities
- CS Table, Monday
- Get Vaccine!
Work for Tuesday
Attendance
- Sam will (attempt to) call you by first name.
- You will respond with
- “Hi, my name is FIRST NAME.
- (Optional pronouns.)
- If you must call me by last name, please call me Mr./Ms./Mx./Scholar
FAMILY NAME.
- I’m taking CSC-282 because …
- This term, I am excited about ….
- (Optional question for me. You’ll also have another chance later.)
Stuff from attendance
Taking CSC-282 Because
- Want to learn more about the powerful tool that Linux represents
- Want to learn more about C and make myself more efficient
- C&Unix are Boomer technology
- FLOSSing your mind
- Navigate around *nix environment
- Refresh C
- Think more efficiently
- Make me a better programmer.
*v A free credit (or at least one credit).
- “I don’t know anything about it.*
- Masochist; wants to suffer through a Sam class [+1]
- Learning about Unix (in addition to in 161)
- Knowledge is good
- “I speak C” (or need to speak C)
- Being strong in one language is better than a smattering
- Fit in my schedule.
- Likes free software
- “I kinda like C”
- “No one can stop me!”
Excited About
- Going back to China.
- Learning more about Unix
- This class
- See people! In person! Vaccinated!
- Graduate! See family!
- Software development
- The term/year finishing
- Being on campus [+1]
- Learn more Unix and its benefits for C programmers [+1]
- Don’t ahve to buy readings.
- Good weather
- Learning about Linux/Unix/Terminal in macOS and such.
Q&A
What’s the least useful *nix utility?
I’ll need to think about it.
If I call you Prof, what will you call me?
Stu or Stew.
Will you tell us a dad joke?
Done.
Do you have to be this snarky?
It’s hard to stop.
What’s the snarkiest thing you’ve ever said?
…
What do we have to download?
Hopefully, nothing. You can do all of this on MathLAN, and I think
you can get to MathLAN at https://remote.cs.grinnell.edu
If you’re on a Mac, you should consider getting the Xcode command-line
tools. xcode-select --install. Typing “gcc” in a terminal window
might work, too.
If you’re on Windows 10, you should install a Windows Linux Subsystem.
(or Cygwin)
About the course
From the reading
Why the course exists
- CSC-161 does not teach enough C
- CSC-161 does not teach enough Unix
- Students asked me to teach this
- Some people like to suffer through Sam classes
- Sam is pedantic
What I hope you get out of it
- Think better about C
- Understand some command-line flags and how to (ab)use them
- Think better about memory and how malloc works
- Get used to building larger C program
- Have you written a C program with multiple files?
- Macros
- Make
- Think better about Linux/Unix/etc., at least from the interaction level
- Important tools: sed, grep, cat, tr, cut, …
- “Thinking in Unix”: Small tools, combined well, using text files (and open)
- Using bash (maybe other shells) and basics of bash scripting
- Other scripting?
- Think better about programming?
- Testing
- Some design issues
- Testing
- Style
Your roles and responsibilities
The class assigns one credit. According to the registrar, that represents
45 hours of work. 45/14 = about three hours per class period. You are
in class for two of those. About one hour outside of class (which will
probably end up being closer to two, but you can stop at an hour)
To get an S
- Show up to at least twelve of fourteen classes
- Participate actively during class
- Attempt homeworks and readings
Some issues this term
- It’s a term! Things will move quickly. Things build.
- Different backgrounds. (Year in Grinnell, experience in 161, etc.)
(Gap since last took C, etc.)
- Be kind to others
- Be willing to take risks
- Sam is trying to be kind to himself - Behind in getting the site set up.
- We can still be successful together.
- And have fun!
Break!
Thinking in C: Your first example
You should know this from the reading, but I’m repeating it anyway.
I’ve reformatted it slightly from the original in K&R.
char *
fun (char *t, char *s)
{
while (*t++ = *s++);
return t;
} // fun
What does it do? (What’s the goal?)
- Copy a string into another string. (From s, to t)
How does it work?
t is “a pointer to the first character in t”
s is “a pointer to the first character in s”
- When we dereference them, we can assign the character
*s = *t
- ++ in each moves on to the next character (e.g.,
*s++).
- pointer++ adds the size of whatever pointer points to to pointer
thereby pointing to the next thing.
- Although most C programmers think of
char and byte as
interchangeable, there’s nothing in the C spec that guarantee
that a char requires only one byte. (Java specifies.)
- Is it important that we do
*t++ rather than *(++t)?
- If we use prefix style, we’ll increment before dereferencing; we
want to increment after dereferencing.
- “When
s is null vs. "When s points to the character 0" vs.
"When s` points to an invalid memory location”.
- How does C mark the end of a string?
- ‘\n’ is a newline
- For files, it’s when
getc() returns EOF.
- The character with value 0 marks the end of a string.
- So, this should stop when
*s is 0.
- How/why does this stop when
*s is 0?
- In C, 0 is “false”
- In C, the value of an assignment expression is the value assigned.
Where does t point after this is done?
- One position after the null character that terminated
t (or part of t).
What are the preconditions? What do we need to know for the code to
work correctly?
- The memory allocated to
t must be at least 1+strlen(s).
- The memory left, starting at
t, in whatever string t
points within, is at least 1+strlen(s).
s is a string (that is, a null terminated sequence of characters)
How do we check the precondition that t is big enough?
- Maybe check within the
while?
-
Maybe check strlen(t)
if (strlen(t) < 1 + strlen(s)) {
return NULL; // Failed!
}
-
Why isn’t that a good strategy? Because it’s wrong. t may not be
a null-terminated string.
char hello[10];
hello[0] = '\0';
printf("%d\n", strlen(hello));
- How much memory is allocated to
hello?
- 10*sizeof(char), so probably 10 bytes
- What does
strlen(hello) return? 0.
- Whoops!
strlen does not report on the amount of memory allocated.
- There isn’t a procedure that tells you how much memory is allocated
to a pointer.
- So, the only way you can check preconditions is by manually looking
at the code.
How can we make it better?
What should we have learned?
- To program in C and to read C, you need to know/remember/understand
details.
- Details include how the various operators work, how C represents various
values (strings, false, etc.) and more.
- C is not a very safe language.
Thinking in C: Your second example
This example is adapted from Kernighan and Plauger, I think.
What does this do?
for (int i = 1; i <= ROWS; i++)
for (int j = 1; j <= COLS; j++)
M[i-1][j-1] = (i/j)*(j/i);
Thinking in C: Your third example
This one will be fun, because I’ll need to figure it out again.
To illustrate my point that understanding memory in C is important,
let’s continue with a problem that a friend gave to me a while ago.
He showed me the following fragment of C code.
x = malloc (...);
foo ();
bar ();
free (x);
The program was crashing on the call to free.
Here are some things they discovered.
- If they removed the call to
free, the program ran through to completion.
- If they moved the call to
free before the call to bar, the program ran
through to completion.
- They had no calls to
free in bar.
What is likely to be wrong with their code? How would you trace the error?