Podcast
Questions and Answers
Which component of a computer system is often referred to as the 'brain'?
Which component of a computer system is often referred to as the 'brain'?
- The Hard drive
- The Kernel (correct)
- The Memory
- The CPU
Which of the following is NOT a typical function performed by the kernel?
Which of the following is NOT a typical function performed by the kernel?
- Memory management
- Hardware manufacturing (correct)
- Process management
- Input/output management
In the context of a computer's file system, what symbol typically represents the root directory?
In the context of a computer's file system, what symbol typically represents the root directory?
/
The command rm
in Linux is used to create a new directory.
The command rm
in Linux is used to create a new directory.
What does the abbreviation 'sudo' stand for in Linux?
What does the abbreviation 'sudo' stand for in Linux?
In the command gcc -o myprogram main.c
, the -o
option specifies the ______ file name.
In the command gcc -o myprogram main.c
, the -o
option specifies the ______ file name.
In C programming, how does a program view memory?
In C programming, how does a program view memory?
Memory mappings are the different sections in which memory is organized.
Memory mappings are the different sections in which memory is organized.
Match the memory sections with their descriptions:
Match the memory sections with their descriptions:
Which memory section is used for dynamic memory allocation?
Which memory section is used for dynamic memory allocation?
The stack grows upwards in memory.
The stack grows upwards in memory.
What is meant by the 'address space' of a program?
What is meant by the 'address space' of a program?
What signal does the OS send when a program tries to access a memory gap?
What signal does the OS send when a program tries to access a memory gap?
Static libraries are shared across different processes.
Static libraries are shared across different processes.
Which type of events happen during program building?
Which type of events happen during program building?
Which of the following is NOT a valid variable type in C?
Which of the following is NOT a valid variable type in C?
How does C handle integer overflow?
How does C handle integer overflow?
printf()
is used to take input from the console.
printf()
is used to take input from the console.
In C, what does the &
operator do when used with a variable?
In C, what does the &
operator do when used with a variable?
Which symbol is used as the format specifier for printing and scanning variables in C?
Which symbol is used as the format specifier for printing and scanning variables in C?
The #include
directive is used to define macros in C.
The #include
directive is used to define macros in C.
What is the purpose of the #define
preprocessor directive in C?
What is the purpose of the #define
preprocessor directive in C?
What symbols are used in a ternary operator?
What symbols are used in a ternary operator?
In C, what are the three essential aspects of a function?
In C, what are the three essential aspects of a function?
What is the purpose of a function prototype in C?
What is the purpose of a function prototype in C?
Passing by value allows a function to operate on the original data of the passed variable.
Passing by value allows a function to operate on the original data of the passed variable.
How does recursion, as a programming technique, typically work?
How does recursion, as a programming technique, typically work?
What is a pointer in C programming?
What is a pointer in C programming?
How much memory does a pointer occupy in a 64-bit system, independent of the data type it points to?
How much memory does a pointer occupy in a 64-bit system, independent of the data type it points to?
In a 64-bit system, an integer always occupies 8 bytes of memory.
In a 64-bit system, an integer always occupies 8 bytes of memory.
The *
symbol is known as the ______ operator and is used to access the data stored at a memory address.
The *
symbol is known as the ______ operator and is used to access the data stored at a memory address.
What is typically stored in a pointer?
What is typically stored in a pointer?
The expressions (*ptr)++
and *ptr++
perform the same operation.
The expressions (*ptr)++
and *ptr++
perform the same operation.
What is a double pointer?
What is a double pointer?
When passing an array to a function in C, what is actually being passed?
When passing an array to a function in C, what is actually being passed?
Pointers are less efficient for array indexing.
Pointers are less efficient for array indexing.
How does the malloc
function work?
How does the malloc
function work?
We use cast type because malloc always returns a pointer to ______
We use cast type because malloc always returns a pointer to ______
Which of the following are equivalent to A[i]
assuming int a[20]?
Which of the following are equivalent to A[i]
assuming int a[20]?
Why is it important to free?
Why is it important to free?
What is a wild free?
What is a wild free?
Flashcards
What is the kernel?
What is the kernel?
Practically the brain of the computer, performing many functions.
Kernel functions
Kernel functions
Acts as a medium between hardware and software; includes memory, process, input/output, and file system management.
What is a filesystem?
What is a filesystem?
How the computer organizes and accesses files.
Root directory symbol
Root directory symbol
Signup and view all the flashcards
ls command
ls command
Signup and view all the flashcards
cd command
cd command
Signup and view all the flashcards
mkdir command
mkdir command
Signup and view all the flashcards
mv command
mv command
Signup and view all the flashcards
cp command
cp command
Signup and view all the flashcards
scp command
scp command
Signup and view all the flashcards
tar command
tar command
Signup and view all the flashcards
rm command
rm command
Signup and view all the flashcards
chmod command
chmod command
Signup and view all the flashcards
cat command
cat command
Signup and view all the flashcards
grep command
grep command
Signup and view all the flashcards
less command
less command
Signup and view all the flashcards
more command
more command
Signup and view all the flashcards
man command
man command
Signup and view all the flashcards
sudo
sudo
Signup and view all the flashcards
gcc
gcc
Signup and view all the flashcards
gcc -o flag
gcc -o flag
Signup and view all the flashcards
How program sees memory
How program sees memory
Signup and view all the flashcards
Memory mappings
Memory mappings
Signup and view all the flashcards
TEXT Section
TEXT Section
Signup and view all the flashcards
DATA Section
DATA Section
Signup and view all the flashcards
BSS Section
BSS Section
Signup and view all the flashcards
HEAP Section
HEAP Section
Signup and view all the flashcards
STACK Section
STACK Section
Signup and view all the flashcards
What is the heap?
What is the heap?
Signup and view all the flashcards
What is the stack?
What is the stack?
Signup and view all the flashcards
Address space
Address space
Signup and view all the flashcards
Accessing memory gap
Accessing memory gap
Signup and view all the flashcards
Static libraries
Static libraries
Signup and view all the flashcards
Shared libraries
Shared libraries
Signup and view all the flashcards
Static events
Static events
Signup and view all the flashcards
Dynamic events
Dynamic events
Signup and view all the flashcards
Integer overflow
Integer overflow
Signup and view all the flashcards
printf()
printf()
Signup and view all the flashcards
scanf()
scanf()
Signup and view all the flashcards
& operator
& operator
Signup and view all the flashcards
Study Notes
- This midterm is fully comprehensive of the content covered so far
- Ensure you fill out the midterm study guide and familiarize yourself with all concepts
- Review labs and codes covered in class; the exam will not include material from zybooks
- Questions regarding covered code may be asked; you won't have to write code
- Understanding how the codes work is essential
- Code will be provided, and you will be asked for the output or to demonstrate operations
- The exam consists of ~30 multiple-choice and ~5 open-ended questions
- The exam is a mix between theoretical and practical questions
Lecture 1
- The kernel of a computer system is analogous to the brain and performs many functions
- Kernel functions include:
- Act as the medium between hardware and software
- Memory management
- Process management
- Input/output management
- File system management
- The file system is represented as a tree, with the forward slash / representing the root
Common Linux Commands
- ls: list
- cd: change directory
- mkdir: make directory
- mv: move
- cp: copy
- scp: secure copy
- tar: tape archive
- rm: remove
- chmod: change mode
- cat: concatenate
- grep: global regular expression print
- less: less is more
- more: more is less
- man: manual
- sudo stands for "Super User Do"
- Sudo elevates privileges to run commands
Compiling Code Using GCC
- gcc is a built-in compiler that is part of the GNU Compiler Collection (GCC) to compile source code into machine code
gcc -o <output_file name> <source_file.c>
compiles code using GCC-o
specifies the output file name
Lecture 2
- A program sees memory as an array of bytes in the range of 0 to 2^64 - 1
- Memory is organized into sections known as memory mappings
- Segments and what they store:
- TEXT: instructions that the program runs
- DATA: initialized global variables
- BSS: uninitialized global variables initialized to zeros until a value is assigned
- HEAP: memory returned when calling malloc/new
- STACK: local variables and return addresses
- Shared Libraries: definitions that are dynamically loaded at runtime and not added to the executable file, so they don't add size to the executable file
- Heap: space for dynamic memory allocation that grows upward
- Stack: stores local variables and grows downwards
- Each program has its own independent view of memory
- Gaps between memory sections may not have any memory mapping
- If a program tries to access a memory gap, the OS sends a SEGV signal, killing the program and dumping a core file
Static and Shared Libraries
- Static Libraries:
- Not shared; each process has its instance
- Represented as .a files added to the executable
- Shared Libraries:
- Shared across different processes
- Only one instance exists for the entire system
- Static events happen during program building while dynamic events happen while the program is running
- Types of variables in C programming:
- int
- char
- double
- pointer
- string
- float
- short
- long
- unsigned
Integer Overflow
- C handles integer overflow by using two's complement and wrapping back to the beginning of the value range
- If 2,147,483,648 is inputed then the output would be -2,147,483,648
Lecture 3
- printf() prints variables and expressions to standard output (terminal)
- scanf() takes input from standard input (console)
- & is the address operator
- When paired with a variable, it returns the memory address
- % is the symbol uses for printing and scanning format specifiers
#include
declares preprocessor directives for using libraries or files within your code#define
is a preprocessor directive to create macros, which are constant values or expressions substituted during compilation
Lecture 4
- A ternary expression is a single-line if-else statement
- Two symbols used in a ternary expression are ? and :
- ? can be considered "if a then b"
- : can be considered "otherwise c"
- Ternary expressions can be read in multiple ways
variable = (condition) ? Expression2 : Expression3;
(condition) ? (Expression2) : (Expression3);
variable = Expression1 ? Expression2 : Expression3;
Lecture 5
- Functions perform a specific task when called, and are made up of three aspects:
- Function declaration
- Function definition
- Function call
- A function prototype tells the compiler about a function's existence and how it can be called
- If a function is not defined at the top of the code without a function prototype a warning indicates calling a function without the compiler knowing what it looks like
- A function prototype tells the compiler a function exists elsewhere, providing its signature (return type, name, parameters)
- Passing by value means function parameters have their values copied, with new memory created for the function call's duration
- Passing by reference allows the function to operate on the original data, modifying the value of a variable or avoiding copying large data structures
- Recursion is a technique where a function calls itself to break down complex problems into simpler ones
- A recursive function typically calls itself in its body
- Recursive function structure: specifying type, function name, arguments, function statements, base condition and the recursive (recursive call)
Lecture 6
- A pointer is a variable that points to an address in memory
- independent of a casted data type, a pointer is:
- 8 bytes in a 64 bit system
- 4 bytes in a 32 bit system
- An integer is 4 bytes in a 64-bit system
- A character is 1 byte
- The * operator is the dereference operator, used to access the data stored at a memory address
- Pointers store a memory address
*ptr = &var
: intializing ptr to the memory address of var- ptr alone produces the memory address of var
- *ptr produces the value at that memory address
- Pointer Arithmetic:
(*ptr)++
dereferences the pointer first and then increments the value contained*ptr++
increments the pointer itself to point to the next memory address- Unary operators read right to left
Double Pointers
- A double pointer stores the memory address of another pointer
- Helpful for managing dynamically allocated memory, passing pointers to functions, or handling complex data structures
- Code breakdown:
int var = 10;
initializes var and assigns it the value 10int *ptr = &var;
initializes the pointer ptr and assigns it the memory address of varint **dptr = &ptr;
initializes the double pointer dptr and assigns it the memory address of ptr- Double pointer dereferencing accesses the contents of var
- When passing an array to a function array decay occurs.
- Instead of passing the entire array, the memory address of the first element is passed
- Need to also pass the size of the array
- Sizeof() returns total amount of bytes
Size of an array can be determined with the following code:
sizeof(array)/sizeof(array[0])
- When passing an array to a function, the function receives the memory address of the first element so the array essentially becomes a pointer
- To iterate an array using pointer arithmetic add a value to move x positions through the array to the pointer
- The expression
*(ptr + 2)
encapsulates the pointer and returns the value at the index that is aimed at
Lecture 7
- The stack stores information for passing control and data and stores:
- Function local variables
- Temporary variables
- Arguments for the next function call
- Where to return after the function ends
- Push is how data is added to the stack
- Removed with pop
- The compiler manages the stack by:
- Creating one stack frame each time a function is called
- Creation when function called
- Stacking on top
- Destroying at function exist
- Stack errors
- Run out of stack space
- Unintentionally change value to memory on the stack
- Access memory even after the frame deallocation
- When stack space runs out, the program crashes and a segmentation fault / stack overflow occurs
- A buffer overflow occurs when more data than anticipated by a program or system process is placed on the stack, causing data to overflow
Array Indexing
- Pointers are more efficient for array indexing because normal indexing requires multiplication, but with pointers there is no need to multiply to get the correct index
- Malloc dynamically allocates a single large block of memory with specified size
- The malloc function returns a suitably aligned pointer to a block of memory for containing any kind of data object
- Memory allocated by malloc is stored in the heap
- Malloc function returns a void pointer, so it will need to be casted to the appropriate pointer type for the data
- A pointers size in a 32 but system is 4 bytes
- There are all equivalent ways of indexing, understand why all of these are equivalent
- Example of this is
int a[20]
A[i]
is equivalent to*(a+i)
is equivalent to*(&a[0]+i)
is equivalent to- Memory previously allocated needs to be deallocated using
free
, to prevent memory leaks - Memory leaks occur objects in memory that are no longer in use by the program but that are not freed
Lecture 9/10 And Beyond
- Premature frees occur when an object that is still in use by the program is freed
- Double frees occur when an object that is already free is freed again
- Wild frees occur when a program attempts to free a pointer in memory that was not returned by malloc
- Memory Smashing happens when less memory is allocated than will be used
- Memory fragmentation: which occurs when there is enough aggregate heap memory, but no single free block is large enough
- Memory can be ran out of in the heap, where malloc returns 0
- A register can ve overwritten in code during execution leading to unintentionally changed heap data
- Memory can be accessed after being freed
- freeing memory twice can cause crashing and corruption
- Creare a memory leak is also a risk when working with memory
- To review are, review multidimensional arrays and how to draw multidimensional arrays, 2D and 3D using double pointers and triple pointers
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.