Podcast
Questions and Answers
Which of the following best describes the role of the kernel in a computer system?
Which of the following best describes the role of the kernel in a computer system?
- It is the primary storage device for user data and applications.
- It acts as an intermediary between hardware and software, managing resources. (correct)
- It manages the computer's network connections and communications.
- It is responsible for rendering graphics and handling display outputs.
The root directory in a filesystem is represented by a forward slash ''.
The root directory in a filesystem is represented by a forward slash ''.
True (A)
Which command is used to create a new directory in Linux?
Which command is used to create a new directory in Linux?
- `mv`
- `mkdir` (correct)
- `ls`
- `cd`
What does sudo
stand for, and what is its primary function in a Linux system?
What does sudo
stand for, and what is its primary function in a Linux system?
In the context of compiling C code with gcc
, what is the purpose of the -o
flag?
In the context of compiling C code with gcc
, what is the purpose of the -o
flag?
A program sees memory as a single contiguous array of bits.
A program sees memory as a single contiguous array of bits.
Match the following memory sections with their descriptions:
Match the following memory sections with their descriptions:
The ______
is a memory area used for dynamic memory allocation, while the ______
is used to store local variables and return addresses.
The ______
is a memory area used for dynamic memory allocation, while the ______
is used to store local variables and return addresses.
The root of the filesystem is represented by a backslash \
.
The root of the filesystem is represented by a backslash \
.
What does the sudo
command primarily achieve in Linux?
What does the sudo
command primarily achieve in Linux?
The gcc -o <output_file> <source_file.c>
command uses the -o
flag to specify the ______ file name.
The gcc -o <output_file> <source_file.c>
command uses the -o
flag to specify the ______ file name.
As seen by a program, what is memory primarily structured as?
As seen by a program, what is memory primarily structured as?
Match the following memory sections with their typical contents:
Match the following memory sections with their typical contents:
What is the primary outcome when a program attempts to access a memory address outside of its allocated memory regions?
What is the primary outcome when a program attempts to access a memory address outside of its allocated memory regions?
Which statement accurately describes how static and shared libraries differ in their utilization?
Which statement accurately describes how static and shared libraries differ in their utilization?
In C, an integer overflow is automatically detected and throws an exception to prevent incorrect calculations.
In C, an integer overflow is automatically detected and throws an exception to prevent incorrect calculations.
How does the #include
preprocessor directive function in C?
How does the #include
preprocessor directive function in C?
Describe the function of a ternary operator and provide a general form of its syntax.
Describe the function of a ternary operator and provide a general form of its syntax.
What is the principal role of a function prototype in C programming?
What is the principal role of a function prototype in C programming?
When passing parameters by _______, the function operates on a copy of the original data, whereas passing by _______ allows the function to directly modify the original data.
When passing parameters by _______, the function operates on a copy of the original data, whereas passing by _______ allows the function to directly modify the original data.
Recursion involves a function calling another function, creating a call stack that grows until a base case is met.
Recursion involves a function calling another function, creating a call stack that grows until a base case is met.
In C, what is the fundamental purpose of a pointer?
In C, what is the fundamental purpose of a pointer?
Match the following terms related to operators and pointers with their descriptions:
Match the following terms related to operators and pointers with their descriptions:
Explain the difference in operation between (*ptr)++
and *ptr++
in C.
Explain the difference in operation between (*ptr)++
and *ptr++
in C.
What is 'array decay' when passing an array to a function in C?
What is 'array decay' when passing an array to a function in C?
The sizeof()
operator, when applied to an array, returns the number of elements in the array.
The sizeof()
operator, when applied to an array, returns the number of elements in the array.
In C, memory allocated using malloc
is stored in the ______ section of memory.
In C, memory allocated using malloc
is stored in the ______ section of memory.
Flashcards
What is the kernel?
What is the kernel?
The brain of the computer. Performs many OS functions.
Kernel functions
Kernel functions
Acts as the medium between hardware and software. Manages memory, processes, input/output, and the file system.
What is a filesystem?
What is a filesystem?
Represented as a tree. It dictates how the computer organizes and accesses files. The root is represented by '/'.
What is 'sudo'?
What is 'sudo'?
Signup and view all the flashcards
What is GCC?
What is GCC?
Signup and view all the flashcards
GCC '-o' meaning
GCC '-o' meaning
Signup and view all the flashcards
Program's view of memory
Program's view of memory
Signup and view all the flashcards
Memory sections are known as...
Memory sections are known as...
Signup and view all the flashcards
Memory Section Storage
Memory Section Storage
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
Program address space
Program address space
Signup and view all the flashcards
Memory gap access
Memory gap access
Signup and view all the flashcards
Static vs Shared Library
Static vs Shared Library
Signup and view all the flashcards
Static vs dynamic events
Static vs dynamic events
Signup and view all the flashcards
What is a ternary?
What is a ternary?
Signup and view all the flashcards
Function definition
Function definition
Signup and view all the flashcards
Function Prototype
Function Prototype
Signup and view all the flashcards
Pass by value vs reference
Pass by value vs reference
Signup and view all the flashcards
What is recursion?
What is recursion?
Signup and view all the flashcards
Study Notes
General Midterm Information
- This midterm is comprehensive.
- Familiarize yourself with all concepts.
- Review labs and codes from class; there will be nothing from zybooks.
- Code understanding will be tested, but coding will not be required.
- The exam includes theoretical and practical questions.
- There will be approximately 30 multiple-choice and 5 open-ended questions.
Lecture 1: The Kernel and Linux Commands
- The kernel is like the brain of the computer system, performing core functions.
- Kernel functions include acting as a medium between hardware and software.
- Memory management is a kernel function.
- Process management is a kernel function.
- Input/output and file system management are kernel functions.
- The file system is represented as a tree.
- The root of the file system is represented by "/".
- The filesystem organizes and accesses files.
- Common Linux commands include:
ls
: list directory contentscd
: change directorymkdir
: make directorymv
: move files or directoriescp
: copy files or directoriesscp
: secure copy (remote file copy)tar
: archive filesrm
: remove files or directorieschmod
: change file permissionscat
: concatenate and display filesgrep
: search for a pattern in filesless
: view file contents page by pagemore
: view file contents page by pageman
: display manual pages for commandssudo
stands for "Super User Do"sudo
allows running commands with elevated privileges.gcc
is a built-in compiler and part of the GNU Compiler Collectiongcc
compiles source code into machine code.- To compile code use:
gcc -o <output_file name> <source_file.c>
-o
specifies the output file name when compiling withgcc
.
Lecture 2: Memory Organization
- A program sees memory as an array of bytes, ranging from 0 to 2^64 - 1.
- Memory is organized into memory mappings
- The TEXT section stores instructions that the program runs.
- The DATA section stores initialized global variables.
- The BSS section stores uninitialized global variables, which are initialized to zeros until a value is assigned.
- The HEAP stores memory returned by
malloc/new
. - The STACK stores local variables and return addresses.
- Shared libraries (.so files) are not added to the executable file and are dynamically loaded at runtime
- Shared libraries consist of definitions only, so they do not increase the executable file size.
- The heap is space for dynamic memory allocation and grows upward.
- The stack grows downwards and stores local variables.
- Each program has an independent view of memory.
- Gaps between memory sections may lack memory mapping.
- Accessing a memory gap results in a SEGV signal, which typically terminates the program and creates a core file.
- Static libraries are not shared and have an instance for each process; they are represented as (.a) files added to the executable.
- Shared libraries are shared across different processes with only one instance for the entire system.
- Static events happen during program building.
- Dynamic events happen while a program is running.
- Variable types in C programming include
int
,char
,double
,pointer
,string
,float
,short
,long
, andunsigned
.
Lecture 3: Operators, and Preprocessor Directives
- With integer overflow, C uses two's complement and wraps back to the beginning of the value range.
- Inputting 2,147,483,648 results to an output of -2,147,483,648 due to integer overflow.
printf()
prints variables and expressions to standard output (the terminal).scanf()
takes input from standard input (the console).- The "&" operator is the address of operator.
- When paired with a variable, "&" is used to acquire the memory address.
- "%" (percent sign) is the format specifier for printing and scanning.
#include
is a preprocessor directive that declares libraries or files for use in the code.#define
is a preprocessor directive and creates macros: constant values or expressions that the preprocessor substitutes before compilation.
Lecture 4: Ternary Operations
- Ternary operations use the '?' and ':' symbols.
?
can be considered as 'then', and:
can be considered as 'otherwise'.- Ternary expressions can be read as:
variable = (condition) ? Expression2 : Expression3;
(condition) ? (Expression2) : (Expression3);
variable = Expression1 ? Expression2 : Expression3;
Lecture 5: Functions
- Functions perform specific tasks when called and contains
- Declaration
- Definition
- Function call
- A function prototype is a declaration that tells the compiler about a function's existence and signature (return type, name, parameters).
- Prototypes prevent implicit declaration warnings, which occurs when a function is called before its definition without the compiler knowing its structure.
- Passing arguments by value results copies the values into function parameters; changing the parameters does not modify the original values.
- Passing by reference allows the function to operate on the original data, allowing modification of the original variable, and avoiding copying large data structures.
- Recursion is a technique where a function calls itself to solve smaller subproblems.
Lecture 6: Pointers
- A pointer is a variable that stores a memory address.
- A pointer uses 8 bytes in a 64-bit system and 4 bytes in a 32-bit system, regardless of the data type it points to.
- An integer uses 4 bytes and a character uses 1 byte in a 64-bit system.
- "*" is the dereference operator and is used to access the data stored at a memory address.
- Pointers store a memory address.
int *ptr = &var;
means*ptr = &var
will point at the memory address of&var
.ptr
alone will produce the memory address ofvar
.*ptr
will produce the value that resides at that memory address.(*ptr)++
dereferences and adds 1 to the value.*ptr++
increments the pointer to the next memory address.- Unary operators read from right to left.
- A double pointer stores the memory address of another pointer.
- Double pointers are useful for managing dynamically allocated memory as well as dealing with complex data structures.
int var = 10; int *ptr = &var; int **dptr = &ptr;
initializesvar
to 10,ptr
to the memory address ofvar
, anddptr
to the memory address ofptr
.- Dereferencing
dptr
twice accesses the contents ofvar
. - When passing an array to a function, or any part thereof, array decay occurs; the array name decays into a pointer to the first element, we are passing the memory address and need to know the size.
Lecture 7: Arrays and the Stack
sizeof()
returns the total bytes of what is in the parentheses.sizeof()
is used to determine the size of the array to get the actual array size by doingsizeof(array)/sizeof(array[0])
.- When passing an array to a function, the memory address of the first element is the parameter.
- When passing an array, it then becomes a pointer.
- Iteration of an array using pointer arithmetic can be done by adding a value to a base pointer to move positions in memory.
- The expression
*(ptr + 2)
produces the value at the index. - The stack stores information for passing control and data and allocating memory, including:
- Function local variables
- Temporary variables
- Arguments for the next function call
- The return address, when the function ends.
Push
is used to add data andPop
is used to remove data from the stack.- The compiler manages the stack by:
- Creating one stack frame each time a function is called.
- Stack frames are created when the function is called.
- Frames are stacked on top of one another.
- Frames are destroyed at function exit.
- Errors that can occur in the stack:
- Running out of stack space
- Unintentionally changing values on the stack (in other function frames or return addresses)
- Accessing memory, even after the frame is deallocated
- Stack overflow occurs when the stack space runs out.
- Buffer overflow occurs when more data than anticipated gets placed by a program or system process to the stack, overflowing the stack
- Pointers are more efficient for array indexing because the normal indexing requires multiplication to find the correct index.
Lecture 8: Malloc and Memory
malloc
dynamically allocates a single large block of memory with a specified size.malloc
returns a pointer to a block of memory and stores the memory allocated by malloc is stored in the heap- The reason for a pointer cast type is because the malloc function returns a pointer to void.
- The size of a pointer in a 32-bit system is 4 bytes.
Lecture 9/10: Memory Allocation
- Ways of indexing:
- Assuming
int a[20]
is defined: A[i]
is equivalent to*(a+i)
and*(&a[0]+i)
- Memory previously allocated by
malloc
orcalloc
is deallocated byfree
. Doing so prevents memory leaks. - Memory leaks are objects in memory that are no longer in use by the program but that are not freed.
- Not deallocating memory leads to memory leaks.
- The heap can run out of space, in which case
malloc
will return0.
- One can unintentionally change other heap data, or clobber heap data by overwriting a register during execution.
- Other heap errors are Accessing memory after it has been freed or freeing memory twice, which could cause crashing or corruption.
- Such heap errors create a memory leak.
Lecture 11: Memory Management Errors
- A premature free is caused when an object still used by the program is freed.
- A double free is caused by freeing an object that is already free.
- Wild frees happen when a program attempts to free a pointer in memory that was not returned by malloc.
- Memory smashing occurs when less memory is allocated than will be used.
- Memory fragmentation occurs when aggregation of memory lacks a single large block.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.