DevPrep

All topics › Programming basics

Memory

Junior

The memory hierarchy

The closer memory sits to the CPU, the faster and smaller it is:

Stack and heap — what you will be asked about

Inside RAM a process has two regions that matter for Swift:

struct Point { var x: Double; var y: Double }  // a value — usually on the stack
final class Node { var next: Node? }  // an object — always on the heap

func demo() {
    let p = Point(x: 1, y: 2)  // 16 bytes in the stack frame, gone on return
    let n = Node()                  // malloc + refcount; dies when count == 0
    var points = [p]  // the array buffer is heap, though Array is a struct
    points.append(Point(x: 3, y: 4))
    _ = n
}

Why it matters on iOS

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store