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
example.cpp
1String a = "hello"; // a owns a heap buffer2String b = a; // COPY: deep copy -> new buffer3String c = std::move(a); // MOVE: steal a's buffer4// 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 stepPlays automatically · Space to play/pause · ← / → to step · controls are above the diagram