1/162
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
How to compile but do not link
gcc -c hello.c
-Wextra
provides extra warnings
How to compile into executable
gcc -o hello hello.c
Compile and link
gcc hello.c
-o
places output into a file whatever the user wants to name
-c
compile into object file but DOESNT link
What type of language is C
Imperative
Header Guard
#ifndef _SOMETHING
#define _SOMETHING
#endif
What is stored on the stack?
Local variables and function-calling sequence
What does Preprocessor do?
Defines macros and libraries.
-file inclusion (#include)
-marco definiton (#define)
-Conditional Compilation (#if, #ifdef, #ifndef, #else, #elif, #endif):
What does Compiler do?
Translates C code into assembly code
What does the Linker do?
Combines object modules and static libraries (archives) into a single load module, which is also in machine language
What is a pointer?
A pointer is a variable that contains the address of some other variable. (* operator - NOT REFERENCE)
What is a reference?
The address that locates a variable within memory. (& operator -> reference operator)
how many process are created here?
for (int i = 0; i < 5; i++) {
fork();
}
2^5 - 1 = 35
What are the different sections of the program in memory?
text, initialized data, BSS, heap, unallocated space, stack
what is stored in text
machine language of the source code
What is stored in 'data'?
initialized global variables
What is stored on the heap
dynamically allocated storage during program execution
What is unallocated space used for?
Extra space for the heap and stack to use as needed
what is stored on the stack
Local variables and function-calling sequence
What is a primitive data type? Provide 3 examples of primitive data types in C.
Primitive data types are supported by the language itself. They are not user defined.int, float, char, double, void
What is the syntax for a function pointer?
What are the five main steps of the program translation process in order?
preprocessor, Compiler, Assembler, Linker, Loader
What does the loader do?
- Part of the OS.- If applicable, links a load module with dynamic link libraries (shared libraries)- Runs the program
What is a reference
The address that locates a variable within memory. (& operator -> reference operator)
What is a dereference?
Gets the value of the item at an address (* operator -> dereference operator
What data type does C use for IO?
char
What is the difference between binary and text IO streams?
Binary- byte-oriented (raw bytes, no interpretation by the library) Text- 0 or more lines containing 0 or more characters
What is a process? Who is responsible for process management?
A program executing in memory that uses the following resources to perform tasks.- CPU time- Memory- Files-. The OS is responsible for process management.
What is preemptive and non-preemptive scheduling?
Preemptive - CPU can be forcibly removed from a process
Non-preemptive - CPU is never forcible removed from a process
Segmentation in memory
an OS technique where a program is divided into variable-sized, logical segments—like code, data, or stack—rather than fixed-size blocks

Virtual Memory
VM is the OS abstraction that provides the illusion of an address space that is contiguous and may be larger than the physical address space. Thus, possible to load entire processes to memory
How does virtual memory work
by allowing a computer to run programs larger than its physical RAM by using a section of the hard drive as an extension of RAM.
Why is segmentation in memory management important
It divides a program into logical, non-contiguous units (like code, data, and stack), allowing for better organization, flexibility, security, and efficient memory usage by enabling independent growth of these units and providing access protection for each segment.
signed
can represent both postive and negative values
unsigned
can represent only postive ints, up to 255.
Lexical analysis
The process of converting a sequence of characters into a sequence of tokens, which are defined patterns in programming languages. The sequence of tokens are called lexemes. This is the first step in the compiler
Lexical analyzer output of postion = initial + rate * 60
<id,1> <=> <id,2> <+> <id,3> <*> <60>
Syntax analysis
uses the tokens produced by lexical analyzer and creates a tree representation comprised of its grammatical components (operations )to check its structure against the rules of a formal grammar (order of operations. operations)
syntax analyzer output of <id,1> <=> <id,2> <+> <id,3> <*> <60>
(draw and tree then check with notes)
Semantic analyzer
checks for the meaning and logical consistency of a program after syntax analysis has confirmed its grammatical correctness. It involves checking for errors like type mismatches or using an undeclared variable, ensuring that statements are logically sound and adhere to the language's rules for control structures and data types.
Compiling Occurence
Preprocessing -> Compiling -> Assembly -> Linking → loader
What does Assembler do?
Translates assembly code into a relocatable object module, which are in machine language. produces relocatable object modules (ROMs)
Intermediate code generation
translates high-level source code into a simpler, machine-independent representation (IR), such as syntax tree, three-address code, or bytecode.
Code optimizer
improves the efficiency and performance of the generated executable code without altering the program's intended functionality.
what is Type systems?
A set of rules that assigns types to language constructs to ensure type compatibility
Abstraction
the process of simplifying complex systems by hiding unnecessary details and showing only the essential features. It involves creating a simplified representation, like a function or class, that handles the complex internal workings so other developers can use it without needing to know the intricate implementation
ADT
abstract data types, a conceptual model for a data type that defines a set of operations and their behavior without specifying how the data is stored or how the operations are implemented.
Union type defintion
a user-defined data type that allows different data types to be stored in the same memory location. Only one can hold a value at a time, key difference between a struct
bitwise operator &
symbol for AND, operator returns a number or BigInt whose binary representation has a 1 in each bit position for which the corresponding bits of both operands are 1.
0010 & 0011 = 0001
bitwise operator ^
Symbol for XOR. compares corresponding bits of two operands and returns a new number where each bit is set to 1 if the bits in the same position are different.
0 ^ 0 = 0
1^1 = 0
1^0 = 1
bitwise operator |
Symbol for OR a binary operation that compares corresponding bits of two operands. It returns a 1 in each bit position if the bit is 1 in either or both operands, and a 0 only if both bits are 0.
0 | 1 = 1
0 | 0 = 0
bitwise operator »
moves the bits of an integer to the right by a specified number of positions. The rightmost bits are discarded, A right shift on an unsigned integer will fill the leftmost bit with a 0. A right shift on a signed integer will copy the leftmost bit and fill in the leftmost bit with that bit after shifting. This operation is equivalent to integer division by powers of two
What are ADTs comprised of
An Abstract Data Type is a conceptual entity composed of:
- data
- operations on that data
enum.
a user-defined data type that consists of a set of named integer constants. It provides a way to assign meaningful names to integer values, making code more readable and maintainable.
Register variables
a type of variable that the programmer suggests be stored in a CPU register instead of main memory to improve performance.
Restrict type.
an optimization hint that can be applied to pointer declarations. It informs the compiler that for the lifetime of the pointer, no other pointer will be used to access the object to which it points.
Why use restrict types
reduces the amount of alias pointers (pointers that point to the same memory address)
alias pointers
when multiple pointers refer to the same memory location. This means that changes made to the data through one pointer will be visible when accessed through another pointer that aliases the same location.
Comma Operator
Not the same as seperating params/variables/etc with a comma
lowest precedence of all operators
Allows you to put multiple expressions where a single expression is expected
for (int i = 0, j = 100; i < 10; ++i, --j) {
// ...
}
first, ++i then —j
malloc
allows a program to request memory from the operating system during execution for dynamic structures and other scenarios.
example of malloc
int* arr;
int n = 5;
// Allocate memory for 5 integers
// n * sizeof(int) calculates the total number of bytes needed
arr = (int*) malloc(n * sizeof(int));free
used to deallocate or release the memory that was previously allocated by the dynamic memory allocation functions such as calloc(), malloc(), or realloc()
example of free
arr = (int*) malloc(n * sizeof(int));
free(arr);realloc
function that resizes a block of previously allocated memory. It can be used to either increase or decrease the size of the block. returns pointer to block.
example of realloc
arr = (int*) malloc(n * sizeof(int));
int new_size = 10;
int *new_arr = (int *)realloc(arr, new_size * sizeof(int));dangling pointer
a pointer that points to a memory location that is no longer valid, often because the memory has been deallocated or gone out of scope
bitfield
a feature that allows for the packing of data into memory more efficiently by specifying the exact number of bits a structure member should occupy.
initialize a union
union Data {
int i;
float f;
char str[20];
};
how to access unions
the dot symbol. Data.i = 10;
size of a struct
sum of members bytes
float size
4 bytes / 32 bit
double size
8 bytes, 64 bit
signed
can be positive or negative. The leftmost bit determines if positive or negative.
Enum properties.
Each constant is assigned a default integer starting from 0. Can be overridden.
Example of enum
public enum DayOfWeek {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
What would the value of WEDNESDAY be?
public enum DayOfWeek {
SUNDAY,
MONDAY=17,
TUESDAY=16,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
17
encapsulation in c
done by wrapping data in a structure, achieved by structs and pointers.
#
converts the actual argument into a string literal
##
concats two tokens
What flag is used to add a directory to the list of directories to be searched for header files.
The -I flag
FILE ,LINE and DATE are examples of
metadata
what is the size of a char
1 byte
bitwise n - 1
all the bits from the rightmost set bit (inclusive) to the rightmost end are flipped. The rightmost set bit becomes 0, and all the bits to its right (which were originally 0s) become 1s.
what is the size of long
4 bytes
Calloc
returns a pointer that has dynamically allocated memory for an array and initialize all its elements to zero.
Use of calloc
ptr = (int *)calloc(n, sizeof(int));N for amount, say 4 then enough memory for 4 ints would be allocated.
unistd.h
Library for execvp().
execvp()
from the library unistd.h, loads the program a POSIX system call used to replace the current process image with a new process image
What is a thread
lightweight process, Threads share executable code, OS level resources, global data, and heap.
What is a zombie process in C
when a process finished the execution but still has entry in the process table to report to its parent process is known as a zombie process.
Why are zombie process bad
uses PID
How to prevent zombie process
The parent process is then supposed to execute the wait() system call to read the dead process’s exit status and other information. This allows the parent process to get information from the dead process. After wait() is called, the zombie process is completely removed from memory.
wait()
The wait() method stops a thread's execution until notify() or notifyAll() is called or a state change, releasing the lock on a resource. If nothing, the child will return.
notify()
What is deadlocking in terms of threads?
This situation arises when two or more threads are blocked indefinitely, each waiting for a resource that the other has locked.
What is race conditions in terms of threads?
This is the most common issue, occurring when multiple threads access and modify shared data simultaneously.
Segmentation fault
a type of runtime error that occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not permitted.
How can segmentation faults occur?
Dereferencing a null pointer, buffer overflow, stackoverflow, dangling pointer and etc