Announcement: This sheet is only helpful for myself, it doesn’t contains anything I think I am good at. Please refer to articles with prefix [RA] for detailed information.
Midterm review notes from All of Programming Chapter 1 - 13
Chapter 0
Everything is a number.
Everything is a number.
Everything is a number.
Abstraction–the separation of interface from implementation.
The interface of something is what it does, while the implementation is how it does.
Chapter 1 Introduction
7 Steps
- Work an example Yyurself
- Write down what you just did
- Generalize your steps
- replace instance with variables
- find repetition
- Test your algorithm
- Translation to Code
- Testing your program
- Debugging it
Chapter 5
Chapter 8 Pointers
1 | // We can change where p points(e.g., p = &y; is legal if y is an int). However, changing the value in the box that p points at is illegal. |
Can we change? => | **p | *p | p |
---|---|---|---|
int **p |
Yes | Yes | Yes |
const int **p |
No | Yes | Yes |
int *const *p |
Yes | No | Yes |
int **const p |
Yes | Yes | No |
const int *const *p |
No | No | Yes |
const int **const p |
No | Yes | No |
int *const *const p |
Yes | No | No |
const int *const *const p |
No | No | No |
Chapter 9 Arrays
Use arrays to initialize structs:
1 | point p = {.x = 3, .y = 4}; |
Chapter 10 Uses of Pointers
in string.h
, strcasecmp
performs case-insensitive comparision.
string -> int:
1 |
|
This convention is common, as it allows for one to write loops which iterate over the array without knowing a priori how many elemnts are in the array.1
const char * words[] = {"A", "cat", "likes", "sleeping.", NULL};
Chapter 11 Interacting With the User and System
System call is a special type of function call that transfers control from the program into the operating system.
Process Creation
When the command shell wants to run another program, it makes a couple of system calls. First, it makes a call to create a new process(fork
). This new process(which is an identical copy of the original, distinguishable only by the return value of the fork
system call) then makes another system call(execve
) to replace its running program with the requested program. The execve
system call takes an argument specifying the file with the binary to run, a second argument specifying the values to pass the new programm as argv
, and a third argument specifying the values to pass for envp
.
Read Mode
Mode | Read and/or write | Does not exist | Truncate | Position |
---|---|---|---|---|
r | read only | fails | no | beginning |
r+ | read/write | fails | no | beginning |
w | write only | created | yes | beginning |
w+ | read/write | created | yes | beginning |
a | writing | created | no | end |
a+ | rea/write | created | no | End |
Functions
Reading files
1 | int ch = 0; // note that it's int here. |
1 | char line[LINE_SIZE]; |
1 | size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); |
Writing files
1 | size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE* stream); |
When transferring data across the network, the program obtains a file descriptor for a socket-the logical abstraction over which data is transferred-via the socket system call.
Another form of interaction that looks like a file is a pipe. A pipe is a one way communication channel between two processes. One process writes data into one “end” of the pipe, and the other process reads from the pipe, obtaining the data that was written in by the first process.
Another way is through device special files. These files are typically found in the /dev directory. For example, /dev/random provides access to the secure pseudo-random number generator provided by the kernel. The /dev/urandom device can be used instead to generate numbers with the same algorithm, but without regard to if there is sufficient entropy available for security-sensitive purposes.