Introduction to Computer Systems
COMP 321
Memory Allocation §
- when is the size to be allocated determined
- compile-time
- run-time
- when does the allocation occur
- compile-time
- run-time
malloc
returns a void *
. If it fails, it will return NULL
.
#include <stdlib.h>
// ...
int *i = malloc(sizeof(int));
int *array = malloc(4 * sizeof(int));
*i = 3;
array[3] = 5;
Memory allocated by malloc
is not initialized.
Space allocated via varaible declaratin is auto-deallocated when exiting scope.
Explicitly allocated memory must be deallocated explicitly using free
. Else, it is freed when the program terminates.
const int *ptr; // pointer to const int
int *const ptr; // const pointer to int
Assignment expression evaluates to RHS.
Assembly Language §
cc -S main.c
Exception Flow §
- async
- interrupt
- sync
- trap
- fault
- abort