cpp_coremedium priority

Move Semantics: Copy vs Move

🔑 Copy duplicates the resource (O(n)); move steals the pointer and nulls the source (O(1)).

1 / 7
a owns a heap bufferSTACK OBJECTSHEAP BUFFERSbuffer 1helloaptr ● size = 5a manages its own heap storage
example.cpp
1String a = "hello"; // a owns a heap buffer
2String b = a; // COPY: deep copy -> new buffer
3String c = std::move(a); // MOVE: steal a's buffer
4// a is now valid but empty (ptr = nullptr, size = 0)

a owns a heap buffer

String a owns a heap buffer holding "hello". The stack object is just a pointer + size; the characters live on the heap.

Tap ▶ to play · tap the dots or Next → to step

← Previous
Templates: Full Specialization, Partial Specialization, Explicit Instantiation
Next →
RVO & Copy Elision