2-types-address-memalloc-web
2-types-address-memalloc-web
Memory, Part I
CS 61: Lecture 2
9/11/2023
Recap of Lecture 1
• User-level processes interact with each other
and the hardware via the operating system—
in particular, via system calls
User- User- User-
//Process X wants to read 32 bytes of data level level level
//from a file that is stored on the SSD. process process process
X Y Z
//First, get a file descriptor that allows
//the process to interact with the file.
int fd = open(“file.txt”, O_RDONLY); Operating
system
(Kernel-level code)
MMU MMU MMU MMU
Hardware
interrup
schedul
schedul
• A process contains in-memory state like:
timer
sysca
(1) (2) (3) (4)
e
ll
e
t
• Program variables (e.g., global variables and local
variables)
• Function code
Flow of time from the
• At any given moment, a particular CPU perspective of the leftmost
executes code from a single entity (either a CPU
user-level process or the OS)
• The OS leverages various hardware features to
maintain order on the computer MMU MMU MMU MMU
...
e.g., a 64-bit
%rcx
Intel CPU%rdx has registers that are 64 bits
(i.e., 8 bytes) large
CPU
RAM
controll
er
ALU
Instructi
on ALU ALU
op output
decode
Byte 0
RAM controller command
Values in C++
• CPU instructions manipulate bits (zeroes and
ones) that are stored in registers and memory
• 8 bits is called a byte
• An “x-bit” CPU has registers that are x bits in size,
e.g., a 64-bit Intel CPU has registers that are 64 bits
(i.e., 8 bytes) large
• A bitConsider
pattern can
this be(i.e.,
16-bit represented in .various
2 byte) pattern . ways
.
Raw bits: 01100111’11000011
Hex: 67C3
Decimal: 26563
...
We’ll talk
int x = 0; //x is an object living
about the
//somewhere in memory;
mapping in a
//the object’s current
few weeks!
//value is 0.
x = 42; //The memory location
//belonging to object x
//now stores the value 42. Stores the
binary
• A computer uses RAM (“random access 4
representation
memory”) hardware to store objects 2 0
of the value
• A modern laptop or desktop has ~8GB—32GB
of RAM
• An iPhone 14 has 6GB of RAM
Byte 0
• A datacenter server has ~16GB—1TB of RAM
(5) Via the terminal shell
Lifecycle or a graphical interface,
the user requests the > bsort in.txt
of a launching of a new
instance of the
Process executable
SSD
(6) The shell or GUI
//Pseudocode to sort an array uses system calls to:
//of length n. (4) The binary (“executable”) • Create a new process
bubblesort(arr, n): arrives on a user’s storage • Read the code and
for(i=0; i<(n-1); i++):
for(j=0; j<(n-i-1); j++): device (e.g., via an Internet static data from the
if(arr[j] > arr[j+1]): download, a USB stick, or a on-disk executable;
tmp = arr[j]; local compilation) place that
arr[j] = arr[j+1];
arr[j+1] = tmp;
information into the
memory region that
(1) Design belongs to the new
the process
program • Have the new
process start running
.cc the loaded code!
(2) Translate
.hh (3) Compile
design to the program
//C++ variables declared outside of
//a function are globals (i.e.,
//visible to any function). They
//have a static duration---they
//are created when the program Address
//launches and destroyed when the
//program exits.
space
Stores info
int g = 42; //Initial value is 42. about active Byte N-1
functions Stack
void increment(bool do_print) { (e.g., the
bool is_even; //This variable is a values of
//local variable: it’s created local
//when the enclosing function variables)
//starts, is only visible to
//that function, and dies when
//the function returns.
g++;
is_even = ((g % 2)==0); Pulled
if (do_print) { from
printf(“%d\t”, g); executable
printf(“even: %s\n”, into RAM
}
is_even ? “true”: “false”); during (6) Static data
}
return 0; Code
} Byte 0