Theme Catppuccin
Make yourself at homeCatppuccin

A softer light for long learning sessions.

Appearance

Loading your preference…

Dark palette

Choose a dark palette to try it.

Light uses Latte. Your choices stay in this browser.

Own the frame

SKETCHBOOK / 02 The resource desk · 2-week chapter

Page 1 of 4: Let the container own the pixels

Modern C++ starts with values

Let the container own the pixels

A vector owns its elements and manages its backing allocation. When the vector is destroyed, it destroys the elements and releases its storage. You do not pair it with delete[]. This is RAII: resource ownership lives in an object whose destructor performs cleanup when its lifetime ends normally or through exception unwinding.

Build ordinary application types out of members that already manage their resources. An image containing a vector does not need a handwritten destructor merely to imitate an older pointer-and-length design. This is the rule of zero. Returning such an image by value gives the caller an owned result; it is not returning a pointer into a vanished local array.

RAII helps with cleanup. It does not prove that an index is in bounds, that a borrowed view is still valid, or that another thread has finished reading. Those remain contracts you must understand and test.

TRY IT IN YOUR STUDIO

Explain why Image needs no custom destructor. Then identify an invariant its public members do not enforce: could width and pixels.size() disagree?

C.20Prefer the rule of zero for ordinary owning values.
From the page to your own work

Your studio assignment.

Make storage and borrowing visible before adding more geometry.

A finish line you can see

An owned image or color buffer with one borrowed-view operation, a mutation/copy test, and your ownership diagram.

  1. Make an independent change to the buffer operation or representation.
  2. Demonstrate that an owned copy and a borrowed view behave differently after mutation.
  3. Explain which operation could invalidate the view without executing an invalid access.
  4. Use container cleanup rather than a handwritten new/delete pair.
Keep the evidence in your weekly notebook ↗
Explain it like an engineer

Try answering aloud before opening the review notes. Use your own implementation as evidence.

What does span<const T> guarantee, and what does it leave to its caller?
  • It borrows a contiguous range and restricts writes through that view.
  • It does not extend the owner lifetime or prevent other aliases from mutating.
  • Reallocation and owner destruction can invalidate it; C++ does not supply a borrow checker.

These prompts support practice; they are not an automatic assessment of your work.

A small creative variation ↗

Add a function that computes the brightest channel through a const span and returns a value, not a stored view.

Optional. Save it for later if this chapter already feels full.

Coming back after a break? ↩

Redraw just two boxes: the owner and its view. Find their declarations and run the borrow/copy test before editing.

Books & principles behind this study +

Original Parallax explanations and assignments, with these works for further reading.

YOUR MARGIN NOTES

Leave yourself a good question.

What changed your mind? What would you try next?

Notes are private to this browser and saved as you write.

A CHECKPOINT, NOT A SCORE

Can you explain the idea and try the exercise yourself?

Completion records your own assessment. It does not compile or test your C++.
Companion source 3 files