Skip to content
Learn Netverks

Lesson

Step 4/36 11% through track

memory-model-preview

Memory model preview

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
beginner

This lesson

This lesson teaches Memory model preview: the syntax, patterns, and safety habits you need before advancing in C.

Automatic Reference Counting manages heap objects—retain cycles with closures are a common interview topic.

You will apply Memory model preview in contexts like: Kernels, drivers, embedded devices, and performance libraries used by other languages.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true).

At the start of the track—complete before lessons that assume you understand the compiled playground.

Before diving into pointers, understand the two main memory regions C programs use: the stack and the heap.

Stack (automatic storage)

Local variables inside a function live on the stack. They are created when the function is called and destroyed when it returns—no free needed.

void greet(void) {
    int count = 1;   /* stack: gone when greet returns */
}

Heap (dynamic storage)

malloc requests memory that stays alive until you call free. Use it when size or lifetime exceeds a single function call.

Addresses and pointers (preview)

Every object has an address. A pointer stores that address: int *p = &count;. Dereferencing *p reads or writes through the pointer.

Important interview questions and answers

  1. Q: Stack vs heap?
    A: Stack is automatic and fast; heap is manual, flexible, and must be freed to avoid leaks.
  2. Q: What is a dangling pointer?
    A: A pointer to memory that is no longer valid—often after free or returning a pointer to a local stack variable.

Self-check

  1. Where do local non-static variables live?
  2. What function releases heap memory?

Tip: Draw boxes and arrows on paper for stack vs heap—visualizing addresses makes pointer lessons in Rust and C much easier later.

Interview prep

Stack vs heap?

Stack: automatic, fast, limited, tied to function calls. Heap: manual malloc/free, flexible lifetime, risk of leaks and fragmentation.

What is a dangling pointer?

A pointer referencing memory that is no longer valid—common after free or returning address of a local variable.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Stack vs heap one line?
  • Who frees malloc?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump