1/217
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
UNIX
Family of OS that derive from AT&T Unix
Includes Linux and its distributions
Unix philosophy
OS should have simple tools
Each tool has a limited, well-defined function
Relationships between programs are stronger than the programs themselves
Main features of UNIX
Plain text for storing data
Hierarchical file system
IPC
Smaller, more numerous programs
Portability
Kernel (UNIX)
Source code of OS
conf: configuration
dev: device drivers
sys: system kernel
h: header files
Components of UNIX systems
Kernel
Development environment (only on older machines)
Commands
Documentation
Commands (UNIX)
sh: shell (command-line)
System and user utilities
Document formatting
Graphics
Communications
Environment variables
Variables added to the command line
$TERM=vt100Sets terminal to lowest common denominator
Corrects an incorrectly set terminal
$PS1Sets the default command for each prompt
$PS2Sets the default command on execution error
DISPLAY (Environment Variable)
Default display identifier for X11 programs
HOME (Environment Variable)
Home directory
IFS (Environment Variable)
Returns the internal field separator used for word splitting
LANG (Environment Variable)
Sets the system language
LD_LIBRARY_PATH (Environment Variable)
List of directories used to build a process image
PATH (Environment Variable)
Search path for commands
PWD (Environment Variable)
Current working directory
RANDOM (Environment Variable)
Returns random integer between 0 and 32,767
SHLVL (Environment Variable)
Counts the number of bash instances
TERM (Environment Variable)
Display type
TZ (Environment Variable)
Time zone
UID (Environment Variable)
User ID
C preprocessor
Text file processor used to parse code
Preprocessor directives
Commands in code starting with a #
Including contents of files (C preprocessor)
Newer versions allow #embed for binary resource inclusion
#include <file>
#embed <file>Conditional compilation (C preprocessor)
#if <condition>
#else
#elif <condition>
#endif <condition>
#ifdef <var>
#ifndef <var>Macro string replacement (C preprocessor)
#define <object/function> <value>Variadic macro (C preprocessor)
Macro with variable arguments
Macro expansion
Replaces macro tokens with replacement text
Strings and comments are ignored
Undefined macro (C preprocessor)
#undef <value>Line number (C preprocessor)
__LINE__File name (C preprocessor)
__FILE__Defined operator (C preprocessor)
defined(<macro>)
defined <macro>Token stringification (C preprocessor)
# -> str(s)Token concatenation (C preprocessor)
##Abort (C preprocessor)
#errorWarning (C preprocessor)
#warningChains headers of the same name (C preprocessor)
#include_nextC compilation process
Source File
Preprocessor
Compiler
Assembler
Linker
Preprocessing
Removes comments
Expands macros and files
Processes conditional compilation
Compiling
C code → Assembly code
Assembling
Assembly code → machine code
Linking
Function calls are linked with their definitions in machine code
Static Linking
All code is copied into one executable file
Dynamic Linking
Only names of shared libraries are added, which are referred to upon execution
Macro
Symbolic variable containing a value, expression, or code
Created with #define
Substituted during compilation
Convention for macro names
All uppercase with underscores
Types of macros
Object-like
Chain
Multi-line
Function-like
Object-like macro
Contains a value or expression
Chain macro
Macro value is dependent on another macro
Multi-line macro
Span multiple lines for readability
Function-like macro
Takes parameters and invoke commands
Pointer
A variable that stores the memory address of another variable
Pointer declaration syntax
<type> *<var_name>&<var_name>Returns the memory address (pointer) of a variable
Dereferencing a variable
*<var_name>
Pointer size
32-bit system: 4 bytes
64-bit system: 8 bytes
sizeof()
Returns the number of bytes of a variable or type
Null pointer
Pointer variable that doesn’t point to a memory address
Void pointer
Pointer variable that has no data type
Wild pointer
Pointers that have not been initialized
Extremely dangerous
Can cause crashes, data aborts, or even data corruption
Dangling pointer
Pointers that were freed
Extremely dangerous
Can cause crashes, data aborts, or even data corruption
Function pointer
Pointer that stores the address of a function
Allows for functions to be passed as arguments
Dynamic invocation
Callback functions
Multi-level pointer
Pointer that points to another pointer
Advantages of pointers
Dynamic memory allocation
Efficient data collection navigation
Easy access to memory
Can create complicated data structures
Better performance with less code
Disadvantages of pointers
Easy to corrupt memory
Difficult to understand
Vulnerable to memory leaks
Accessing pointers takes longer
Segmentation faults can occur
Dynamic memory allocation
Memory can be allocated, resized, and freed during runtime
Memory is manually controlled
Memory is stored in a heap rather than a stack
malloc(<bytes>)
Allocates <bytes> contiguous bytes of memory on the heap at runtime
Memory is uninitialized
calloc(<bytes>, <var_size>)
Allocates <bytes> * <var_size> contiguous bytes of memory on the heap at runtime
Memory is initialized to 0, or NULL if it fails
free(<pointer>)
Releases memory to the OS
Avoids memory leaks
Best to set it back to NULL
realloc(<pointer>, <bytes>)
Resizes previously allocated memory block
If it fails, memory is not freed automatically
Use NULL handling
Issues with dynamic memory allocation
Memory leaks
Dangling pointers
Fragmentation (inefficient use of heap memory)
Allocation failures (crashes)
struct <name> {};
Creates a blueprint for data collection
No functions allowed
Each member has its own memory
Dot operator for property access
Union
Members share the same memory
Only one value may be used at a time
Union declaration
union <union_name> {}
Union invocation
union <union_name> {}Danger with manipulating union values
Only the last assigned member holds a valid value
Size of a union
The size of its largest member
typedef <existing_type> <new_type>;Allows for user-defined datatypes through aliases
Bitfield
Variables that have a fixed size
Bitfield declaration
<data_type> <member_name> : <width>;
Should be declared in a struct
Uses for bitfields
Saving storage space
Transmitting data
Encryption
Downsides of bitfields
Can lead to overflow or underflow if values exceed bit allocation
Cannot be put into an array
UNIX file abstraction
A file is a sequence of bytes
Everything is a file
Danger with printing variables
Variables must be printed through format specifiers
int size
2 or 4 bytes
float size
4 bytes
double size
8 bytes
char size
1 byte
double format specifier
%lf
Unsigned integer format specifier
%zu
Extended keywords
Controls the memory size of a variable
short (extended keyword)
Narrows value to 2 bytes
unsigned (extended keyword)
Removes negative values but doubles the range
2 or 4 bytes (depending on system)
long (extended keyword)
Widens int to 4 or 8 bytes
Depends on system
long long (extended keyword)
Forces int to 8 bytes
long double (extended keyword)
Offers more precision than double
short int format specifier
%hd
unsigned int format specifier
%u
long int format specifier
%ld