SoftSys Lecture Notes, Monday 2/8/21 #
Announcements #
Homework 1 is due tonight - ask for help on Slack or go to CA hours if you’re stuck.
Check that @softsys-2021-01-instructors and @softsys-2021-01-assistant are collaborators with write access on your fork.
- If not, add them with write access.
- You may need to join the olincollege GitHub organization first.
- About 25% of you have not yet done this - please make sure you do so we can see and grade your work!
Readings #
We’re considering an experiment with the reading.
In terms of technical content, Head First C contains a solid foundation for the course topics. But its presentation style doesn’t sit well with some people (including your instructor).
There are other books you can use, available for free on Canvas - but you have to match the topics up with those of Head First C.
We’re also considering writing our own book for this course, but we could use your input.
We’ll be using a HedgeDoc to keep collaborative notes on the readings, which also serves as a good basis for feedback on the content/presentation.
If you’re interested in spending all or part of the summer helping us expand these notes into a book, talk to us - you’ll be credited appropriately, of course.
Also, if you’re interested in spending a project researching and working on part of the book, you have our support.
In-Class Quizzes #
We’re also going to try an experiment with in-class quizzes.
In part, this is so I can get some data on how everyone is tracking the concepts so I can adjust time spent on topics if need be.
Accordingly, quizzes cover everything we’ve seen in class up to that point (including readings due that day).
Quizzes will be on Canvas, last 30 minutes (start of class) and be open-everything (no searching for solutions, though).
If you need accommodations, let me or Adva know.
We go over the answers immediately after.
There are no makeup quizzes, but you do get to drop your two lowest quiz grades.
I’m not weighting these heavily in your final grade. Again, this is mostly for data collection.
Exercises from Last Time #
A couple of the exercises didn’t get a ton of time allocated last time.
So, you can have some time now to do them.
Spend ~20 minutes trying the Linux Tutorial or Compilation Exercise.
Head First C, Chapter 2 #
Here are some concepts that may be helpful to remember:
- The stack grows downwards in memory addresses, while the heap grows upwards.
- The arrangement of memory on most systems is, from highest to lowest: stack, heap, globals, constants, code.
- You can pass a pointer as a function parameter, which is useful for a variety of reasons.
- You can use the
&
operator to get the address of a variable, and use%p
inprintf
to print it. - If you pass strings (or arrays generally) as function arguments, beware that they actually get passed as pointers.
- The
sizeof
operator gives you the size of the array in bytes for arrays, and the size of the type for anything else. - Arrays are not quite pointers when it comes to
sizeof
and the address (&
) operator. - String literals cannot be updated, but you can make a copy.
- Pointers have types so the compiler can adjust pointer arithmetic properly.
scanf
andfgets
have different uses and tradeoffs - for reading a single string, usefgets
, and for reading structured data, usescanf
.- Segfaults (segmentation faults) are the most common kind of memory-based error, but you might occasionally see a bus error.
Hexadecimal #
Locations in memory are identified by addresses.
In a 32-bit machine, addresses are 32 bits long, so there are 2^32 addresses. In a 64-bit machine, addresses are 64 bits long, so there are 2^64 addresses.
How should we write addresses?
- Binary numbers can get very long (64 digits in a 64-bit system) and difficult to read (they consist only of 1s and 0s).
- Decimal numbers are shorter, but it’s hard to reason about powers of 2.
- Hexadecimal is a sweet spot: two of them make a byte, it’s relatively easy to show powers of 2, and they’re reasonably short.
A hexidecimal digit is 4 bits, from 0
(0000
in binary) to f
(1111
in binary, or 15 in decimal).
We typically use a prefix of 0x
to indicate that what follows is a hexadecimal number.
32-bit addresses then go from 0x00000000
to 0xffffffff
.
Hexercise (Hexadecimal Exercise) #
Try the following questions on your own or with someone else. Try to do most of these without using a computer, but if you need one, there’s a nice one linked below.
- What is
0xb
in binary? In decimal? - What is
0x111
in decimal? - What is
0xbeefface
in decimal (you can use online resources for this one). - What is
0xface
in binary? - What is the longest word you can spell with only hexadecimal digits?
For translating between representations, I strongly prefer Paul Schou’s translator: https://paulschou.com/tools/xlate/
Pointers #
A variable has 4 components:
- A name, like
x
. - A type, line
int
. - An address, like
0xbeefface
. - A value, like 42.
The value is stored at the address.
When x
appears on the right-hand side of an assignment, like int y = x;
, the compiler goes to the address of x
and gets its value there.
When x
appears on the left-hand side of an assignment, like x = 12;
the compiler writes 12 at the address of x
.
Once the program is compiled, the name is no longer used, only the address.
A pointer is a variable whose value represents an address. You can declare it like this:
int *p;
Or like this:
int* p;
You can get the address of an existing variable with &
, like &x
, and you could store this address in a variable by combining the above:
int *p = &x;
You can point to an array or one of its elements, too.
You can dereference a pointer with the *
operator.
When *p
appears on the right-hand side of an assignment, like int y = *p + 1;
, the compiler is going to get the value of p
, go to that address, and get the value there.
When *p
appears on the left-hand side of an assignment, like *p = y;
, the compiler gets the value of p
, goes to that address, and writes the value of y
there.
The malloc
function (which we’ll see more of later) allocates some space on the heap and returns a pointer to that space:
p = malloc(sizeof(int));
Exercises #
Exercise: suppose you have the following array:
int array[10];
Try a few ways of writing a line assigning a pointer to the array. Then try a few ways of writing a line assigning a pointer to a specific element in the array.
Bonus Material #
The memory unsafety anthem: https://www.youtube.com/watch?v=G7LJC9vJluU
Here’s a fun video explaining pointers: https://www.youtube.com/watch?v=mnXkiAKbUPg