Review of C Programming on Unix C problems are often due to misunderstanding the computer Most complaints I hear about C are not about C Most arguments I hear against C are "x detail should be hidden from programmers" Tools: man gcc gdb vim/emacs/nano/pico/gedit/whatever make git C versions: C99-style "for" loop Nested functions and GNU C Older conventions are still around Has not changed as much as C++ Memory, virtual memory, and organization It's a review, but CS253 was a while ago Continuous 64-bit block of addresses Segments: Stack, Heap, Code Shall we print an address from each? In the kernel, these rules *mostly* apply - Stack can be very limited in the kernel - Kernel dynamic memory must be managed carefully Address alignment and performance - Can turn this off Pointers: Pointers are 64-bit unsigned integers Pointers can point to any type, since it's just a number Use size_t for data structure sizes ssize_t for signed size_t (why is this ok?) sizeof returns the size of variables Pointer arithmatic example: Iteration - Example: Sum of an array - Example: Encrypt a string with shift of 13 (if time permits) - Example: Square each number in an array - Incrementation amount determined by type - Either direction is possible void* can point to anything! Programs can write to anywhere they're allowed to write to - Example: Change a different variable Passing pointers to functions: Sort of a manual pass-by-reference Allocating Memory: malloc and free calloc Don't forget to deallocate! Arrays vs. Python lists, and realloc - Userspace: C++ vector - Kernel: Kernel linked lists goto: Actually used a lot in the kernel Example: How often? Considered harmful - Which is harmful Pointers to functions: CS253 reminder: We used these with pthreads, and probably other places Now we will do things that are more silly! An array of pointers to word-scrambling functions A function that takes a list of functions to apply to a word A function that returns a function? get_scrambler Ok, that was silly, but how about something that actually happens? Structs with function pointers: Kind of like an object We will actually have to make these when we put files in /proc Project 1: Make one and hand it to the test function An example that was in 253: sigaction We'll make a program that handles ctrl+c again Resources: A guide to GDB: http://beej.us/guide/bggdb/ Make (Wikipedia): http://en.wikipedia.org/wiki/Make_(software) Pointer arithmatic overview: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html Pointers (Wikipedia): http://en.wikipedia.org/wiki/Pointer_(computer_programming)