13. Structs and Constants

SoftSys Lecture Notes, 3/22/21 #

The 2038 Problem #

2038 will pose some interesting problems, not just because we might all be underwater at that point.

Course Planning #

Believe it or not, we’re at about the halfway point of this course.

Due to a variety of circumstances, we’re a day or two behind where we might be in a normal semester, which is okay.

However, this means that for the sake of everyone’s sanity, we will have to shorten or eliminate a few parts of the course. Here are some examples of what we could do:

  • Make some homework assignments optional and cover them more quickly in class.
  • Cut out some homework assignments entirely and skip a few topics.
  • Shorten and/or cut the second project and focus the remainder of the course on readings and homework.

All of these were done last spring, and at a minimum, a second project will have to be shortened if it happens, due to the extended deadline of the first project.

Shapes #

To help you get a bit more practice with memory allocation, here is an exercise. In the ex05 folder of your course repository, you should see a file called shapes.c. Open it and follow the instructions in the comment at the top of the file.

To test your code, compile and run test_shapes.c. Make sure to also check for memory leaks by running Valgrind.

Constants #

When you pass data to a function that should not be changed, you can use the const keyword to indicate this. If you then try to change that data in your code, you will get a compilation error (which can check that you are trying to change the value of a const type).

For pointers, this can be a bit tricky. Again, the right-to-left rule can come in handy.

int* const x; means that x is a constant pointer to an integer. Thus x is a pointer, but you cannot change the address that x is pointing to. However, you can change the value that x is pointing to.

const int* x; means that x is a pointer to a constant integer. You can change the address that x points to, but you cannot change the value pointed to by x.

const int* const x; means that x is a constant pointer to a constant integer. This means that you cannot change the address or the value that x points to.

In a function, you should consider whether a pointer or its value needs to be modified, and use the appropriate form of const. If you do this and the compiler warns you about modifying a const value, it’s usually worth it to go through and check where this is happening. If you really need to modify a const value, you can cast the pointer to a non-const type first.

As an exercise, modify your shapes.c code based on to use const as appropriate. You will likely also have to modify shapes.h and test_shapes.c as well.

Midpoint Survey #

We’re officially halfway through the class meetings of this course.

I’d really appreciate it if you took a moment to provide feedback.

There is a midpoint survey link on Canvas, and the link will be posted on Slack as well.

For Next Time #

Read The Little Book of Semaphores, Chapters 1 and 2.

Get and test Sync using the following steps:

  1. Clone https://github.com/AllenDowney/LittleBookOfSemaphores
  2. cd into the code folder.
  3. Run python Sync.py sync_code/mutex.py to try it out.

Continue working on your project and be sure to get an update done by next week.