211 Midterm II Review
211 Midterm II Review
Pointers
Memory Organization
● 1 byte = 8 bits
● Each address consist of a byte
● Range of addresses: [0, N-1], where N is a positive integer value (typically 2^32 or 2^64)
● Recall the size in bytes for an int, short, and char
○ int = 4 bytes
○ short = 2 bytes
○ char = 1 byte
● The number of address spaces a value needs corresponds to the size of bytes of its data
type
○ int x = 4 -> requires 4 address spaces
○ short y = 2 -> requires 2 address spaces
○ char ch = ‘s’ -> requires 1 address space
Pointers
● & - fetches the address of a variable
● * - dereferences a pointer (i.e retrieves the actual value its pointing to)
● A pointer is 4 bytes in a 32-bit system, 8 bytes for 64-bit system
● Format specifier for printing pointers: %p
○ But you’re essentially printing the address its pointing to
● If you set the value of a pointer, it also changes the value of what it’s pointing to
Arrays and Pointers
● Basically the same thing
● Suppose we have the array: int a[5] = {1, 2, 3, 4, 5} and a pointer: int* p = a
○ a refers to the address in memory where the array starts
○ a[1] == p[1] == *(p+1)
○ a[k] == p[k] == *(p+k)
● If I did *p = 1, then:
○ *(p+1) refers to the 2nd element of a
○ *(p+k) refers to the k+1 element of a
Memory Allocation
Program and Memory
1. Stack
a. Local variables
2. Heap
a. malloc() and free()
b. # malloc() calls = free() calls
3. Read/Write Data Segment
a. Global Variables
i. int N = 4
b. Static Variables
4. Read Only Data Segment
a. Machine instructions
b. Constant Global Variables
i. const double PI = 3.14
c. String literals
GNU Compiler Collection
Three Compilation Steps
1. Compile Step
a. Translates a C program -> assembly program
b. Steps
i. gcc -S p1.c
ii. p1.s assembly program is created
1. Set of text instructions that are used to program the processor
2. Human readable
Three Compilation Steps
2. Assemble Step
1. R-type instruction
a. R-type involves two registers
b. An example: add
c. Opcode will always be 000000
2. I-type instruction
a. I-type includes Intermediate value, easy to remember
b. Some examples: addi
c. Opcode will always NOT be 000000
Assemble, Link, and Load
ELF Sections
● .text: machine instructions
● .rodata: read only data (constants)
● .data: initialized global/static variables
● .symtab: stores the following:
○ Name/address locations of
○ Global variables
○ Static variables
● .rel.text: relocation info for .text section
● .rel.data: relocation info for .data section
Assemble Step
● Creates an ELF file
● Creates >= 1 relocatable object files (.o)
● Creates the symbol table in .symtab
● Remember that calls like malloc(), free(), and printf() are also entries
Linker Step
1. Symbol Resolution
a. Linker attempts to locate the same symbol with same name and section type for each
unknown address in the symbol table
2. Relocation
a. Copying from relocatable object file(s) -> executable file
i. What are we copying? .text and .data from relocatable
ii. What are we copying to? .text address locations in executable file
Loader
Ok, so now we have the executable file from the Linker step, what do we do now?
How? ./a.out
Remember this?
● Includes a library symbol table that lists each symbol defined by a member in the archive
that is a relocatable object file
Key Observations
Occurs after program is loaded into memory (load-time) or, when the program is executing instructions (run-time) in memory
This results in a much smaller file size compared to Static Linking, which will be shown in the next slide
Dynamic Linking Pt 2
Key Observations: