Surviving the C: A Systems Programming Raft for Beginners

I took Advanced Programming at Columbia University this past semester, and it was by far the most difficult class I’ve taken at this school in my undergraduate career. We covered a vast amount of material in one semester, and really dove deep into learning C and systems programming. We also covered the basics of UNIX, networks, version control using Git, and skimmed C++.

I came into this class only knowing Java and a little bit of Python, with some Data Structures knowledge. I came out of it knowing much, much more, and became a more independent programmer as a result. It was a very challenging process: I realized how important it was to talk through my ideas with others, and seek help when needed! Doing things alone not only causes unnecessary frustration, but makes it almost impossible to do hard projects! Overall, it was definitely a journey of self-learning and pushing past my limits.

While the class itself set what we would learn, I did the vast majority of my learning outside the classroom. If you can, I would actually not recommend taking this class as the teacher makes it so that you have to devote your life to it. Learn this material on your own time, you’ll find it a lot more fun! 🙂

Also, this class is incredibly important, as many people including my mentor, who’s worked as a software engineer for 20 years, would tell you. This class really lays the foundations of programming, because you learn about pointers, memory management, and what really happens under the surface. Because the course is taught in the languages of C and C++, you’re forced to think about every little step you’re doing. Although this seems like drudgery with all the new frameworks out there, it will no doubt be invaluable to any aspiring software developers or engineers out there. My mentor’s first job out of college, for example, involved programming in C/C++ in a UNIX environment. Git is also used everywhere as a version management system now.

Here’s a brief overview of the topics I learned, as well as completed assignments, over the semester:

  • The intricacies of C

Assignment: implement a linked list in C

  • Makefile
  • Pointers
  • File I/O in C

Assignment: Look up messages in a database and print to the user

  • UNIX and process hierarchy
  • TCP/IP networking

Assignment: Write a shell script that executes a pipeline that transforms our lookup program into a server.

  • Fork/exec

Assignment: A different way of creating a server lookup program.

  • Pipes / sockets when building a lookup network server for a database

Assignment: Create a server lookup server using the sockets API

  • HTTP protocol and building a very simple web server

Assignment: Create a webpage downloader similar to wget on the command line. This downloader can download a single file.

Assignment: Build a very simple web page, then write a web server that serves static HTML and image files.

Assignment: Link the web server with the lookup program from earlier, so clients can lookup messages from a browser.

  • Stack vs Heap Allocations
  • Pointers in C vs References in C++
  • C++ and the Big Four

Assignment: Implement operator overloading for our own String class in C++

  • Templates and Containers in C++

Assignment: Using the linked list from earlier, rewrite it as a template class so that it can take in other data types rather than just Strings.

  • Smart Pointers
  • Basics of using Git
  • Debugging with Valgrind

 

I’ll also speak about some functions and concepts that I found invaluable for the class and recorded in my READMEs along the way! They either are 1. constantly used or 2. learned only through my experience and/or research online.

UNIX commands (with some additions from Eric, thank you !)

  1. rm -rf  abc = removes files and folders from the directory abc
  • don’t type “rm -rf /”!!!
  1. mv filename dir-name = move a file to a sub-directory
  2. renaming a file – mv data.txt letters.txt
  3. pwd — Path to Working Directory; or, where the Hell am I?
  4. clear — clear all text from the terminal
  5. id — User ID; or, who am I?
  6. ps -ef | grep myprocess — is process myprocess running? What is its PID, so that I may murder it?
  7. ls -larth — show me everything about the contents of this directory, especially the size of the files in a human-readable form [helpful to alias this one to ‘ll’])

Vim commands with many additions from Eric!

How to delete a large block of lines on Vim:
Go to the starting line and type ma (mark “a”). Then go to the last line and enter d’a (delete to mark “a”). That will delete all lines from the current to the marked one (inclusive).

Also try:

d3d — Delete 3 lines (or, use any other number)

cw — Change a word

c4w — Change 4 words

:x! — Save and quit (same as wq!,)

ZZ — same as :x!,

:.,+4s/onething/anotherthing/g — on the current line, and the next four lines, replace every single instance of onething with anotherthing

Makefile

A Makefile essentially is a file that runs the commands you have pre-typed into it. So instead of typing “gcc …” to compile, you could simply type “make” into your command line, and “make” would refer to the line in your Makefile that tells the compiler what files should be compiled, then linked.

example:

sample: sample.o

sample.o: sample.c

A good Makefile reference from our recitations: https://github.com/cs3157/recitations/blob/master/B-Makefiles/sample-Makefile

Allocating memory/freeing memory

In C, use “malloc” but be careful when hard-coding how many elements you want to allocate on the heap. Remember to “free” your heap memory once you use it. In C++, this would be equivalent to using “new” and “delete”.

Useful functions when dealing with “strings”, or char arrays, in C

strcpy/strncpy

strstr

strcat/strncat

memcpy

Friend vs Member Functions in C++

Member functions are functions that can access your private data, meaning they exist within your class or struct. Thus, when you want a function that exists outside of your class/struct, you wouldn’t make it a member function.

However, what if you want a function outside the class or struct to use the private data of the class or struct? Then, you would use a friend function.

Remember this joke: only you and your friend can touch your private parts!

Difference between Copy Constructor and Copy Assignment in C++

The COPY CONSTRUCTOR — called when a new object is created from an existing object, as a copy of this existing object.

THE COPY ASSIGNMENT — called when an already initialized object is assigned a new value from another existing object.

Final comments

 The most important part of this class (and life) was knowing when to start and stop.

For incoming AP-ers, I would recommend two long chunks of time to devote to coding, and a few other smaller blocks of time to do tasks like coming up with an action plan and cleaning up your work so you have a nice working executable to submit.

A week before

  • Start each lab early! Read each lab instruction a week before the lab is due.
  • Write out for yourself what the specifications are for the executable you want to produce.
  • Come up with an action plan.
  • Let your brain relax and do something else, so in the back of your mind you’re focused when you actually begin the lab.

4-6 days before lab due date

  • Create pseudocode for the lab
  • At this time, allocate a solid block of time to code (Chunk #1)

 2-3 days before lab due date

  • Hopefully at this point you’ve gotten the general gist of the lab and you’re debugging the errors (you’ll have many memory errors!) in your code. This will be Chunk #2 of time that you’ll need to allocate.

 0-1 days before lab due date

  • Do your last debugging, write your README, comment your code, make your working directories clean, and do your last Git commits.

Also, make sure to pay attention to what the TAs and professors say to focus working on and studying on over email. It’s a deliberately difficult course. There’s a lot of material and little time to spend on studying.

Make sure to familiarize yourself with the man (manual) pages for the functions that you’ll be using by typing “man <program>” into Google. Or to be honest, I would recommend explanations on more understandable websites like TutorialsPoint. Understand what each one does. That way, you won’t have to spend too much time parsing through other peoples’ code on Stack Overflow trying to look for one that exactly matches what you’re doing. That being said, Stack Overflow is a very good friend who can often explain concepts well, as long as you really tried to figure out the answer beforehand.

Looking back on it, I learned so much in this class, and I definitely feel tougher and a better programmer as a result. I hope this post will help future new systems programmers! 🙂

Leave a comment