1/27
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
Core rules of C
All data has a place (somewhere in memory)
All data has a size (sizeof tells byte size)
Zero is special (end of strings and NULL)
built in versions of zero
‘\0’ for char
0 for int
Differences of Java and C
Java is memory-managed, C is not
Java is object-oriented and strongly types, C is not
Java has built-in dynamic data structures, C does not
What ‘C programming is procedural’ means
No classes or objects
use of Global and local variables
No exceptions but uses error codes
has small, well-defined functions
Memory management
When the system manages your dynamically-allocated memory
Advantages of memory management
Few details for programmer
Code focuses on problem not allocation
reduced risk of memory-related security issues
Disadvantages of memory management
Less control and knowledge of what your program is doing
difficult to identify performance issues by studying code
difficult accessing hardware-specific low-level memory and registers
Characteristics of interpreted languages
Runs within virtual machine providing standard environment
Cross-platform compatible
greater overhead therefore worse performance
Characteristics of compiled languages
Native hardware instructions and conventions
needs compilation for different platforms
less overhead therefore better performance
Undefined behaviour
Behaviour, upon use of a nonportable or erroneous program construct or of erroneous data, for which this international standard imposes no requirements
Low-level and high-level languages
High level language abstracts more details that are irrelevant to the problem
Low level language requires programs to pay attention to irrelevant details
Static memory
Where memory allocation size is known at compile-time
Dynamic memory
Where memory allocation size is not known at compile-time
Places in which data must be located
The stack
The heap
The data segment
The stack
Read-write, created/destroyed with function calls. Requires size known at compile-time
The heap
read-write, allocated from OS via syscall. Can handle size unknown at compile-time
The data segment
read-only, stores literals and constants within the process
Data sizes in C
char: 1 byte
short: 2 bytes
int: 4 bytes
float: 4 bytes
long: 8 bytes
double: 8 bytes
Arrays in C
Laid out sequentially in memory
can be static or dynamic
they use pointer arithmetic
Decaying an array into a pointer
This is when a static array is passed to a function. the pointer to the starting element is passed and not the array as a whole
C compilation process
Preprocessing
Compilation
Assembly
Linking
Preprocessing
Prepares the raw code by removing comments, expanding macros and copying in header files.
Input: source code .c
output: expanded source code .i
Compilation
Checks the expanded code for syntax errors and translates high-level C logic into low-level instructions
input: expanded source code .i
output: assembly code .s
Assembly
Assembler translates human-readable instructions into raw, binary machine code the CPU understands
Input: assembly code .s
output: object code .o or .obj
Linking
Combines generated object code with external libraries and other object files to build final program
input: object code .o + external libraries
output: .exe or a.out
Declarations and Definitions
Declaration of something indicates its name and type
Definition of something indicates its name and type and reserves memory for it
When to use a Struct
This structure is used when you need to group related pieces of data that must all be stored maintained, and accessed simultaneously
When to use a Union
This is a structure used to conserve memory when having a variable that may be one of several types