Move Semantics at the Machine Level
Move semantics arrived in C++11 to put a stop to needless copies. The marketing pitch: std::move "moves" data instead of copying it. So what does "move" actually do at the assembly level? This lesson opens the bonnet on std::move, demonstrates that RVO erases copies altogether, and lines up real copy-vs-move assembly side by side.
What std::move Actually Compiles To
Here's the part that surprises people:
#include <utility>
void example(std::string &s) { std::string a = s; // Copy std::string b = std::move(s); // Move}What does std::move itself compile to?
; std::move(s) compiles to... nothing. ; It is a cast. Specifically: ; template<typename T> ; constexpr remove_reference_t<T>&& move(T&& t) noexcept { ; return static_cast<remove_reference_t<T>&&>(t); ; } ; A cast produces no instructions. It just changes the type.std::move is a cast to an rvalue reference. Casts emit no instructions; they only steer which overload — copy constructor or move constructor — the compiler picks.
What changes is what runs AFTER the cast — which constructor gets called. (The long symbols are GCC's names for std::string under the C++11 string ABI, std::__cxx11::basic_string<char>; older material shows _ZNSs... names from the pre-C++11 string.)
; Copy: calls string(const string&) - copies all data mov rdi, rax ; 'this' for new string mov rsi, rbx ; Source string (const ref) call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1ERKS4_ ; string::string(const string&) ; Inside: allocates a new buffer if the text is too long for the inline one, copies all characters
; Move: calls string(string&&) - steals the buffer mov rdi, rax ; 'this' for new string mov rsi, rbx ; Source string (rvalue ref) call _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EOS4_ ; string::string(string&&) ; Inside: takes the heap pointer (or copies the short inline text), resets the source. No allocation!Inside the Move Constructor
Start with a textbook heap-only string: a data pointer, a size and a capacity, no small-string buffer. Its move constructor does this:
; string::string(string &&other); rdi = this, rsi = otherstring_move_ctor: ; Copy the buffer pointer, size and capacity mov rax, QWORD PTR [rsi] ; other._data mov QWORD PTR [rdi], rax ; this._data = other._data mov rax, QWORD PTR [rsi+8] ; other._size mov QWORD PTR [rdi+8], rax ; this._size = other._size mov rax, QWORD PTR [rsi+16] ; other._capacity mov QWORD PTR [rdi+16], rax ; this._capacity = other._capacity
; Null out the source (so its destructor doesn't free the buffer) mov QWORD PTR [rsi], 0 ; other._data = nullptr mov QWORD PTR [rsi+8], 0 ; other._size = 0 mov QWORD PTR [rsi+16], 0 ; other._capacity = 0 retNine mov instructions, total: three pointer-sized values copied across (a load and a store each), three zeroed on the source. A copy would be malloc plus memcpy plus bookkeeping, easily hundreds of instructions on a fat string. The gap widens linearly with data size.
That's the textbook picture. libstdc++'s real std::string adds the small-string optimisation: strings of up to 15 characters live in a buffer inside the object, so moving one copies those bytes, and a moved-from string is left pointing at its own empty inline buffer rather than at null. std::vector has no inline buffer, so it's the clean three-pointer case below.
Copy vs Move: Side by Side
std::vector<int> create_large_vector() { std::vector<int> v(1000000); return v;}
void copy_version(const std::vector<int> &src) { std::vector<int> dst = src; // Copy: allocates 4MB, copies all data}
void move_version(std::vector<int> &&src) { std::vector<int> dst = std::move(src); // Move: copies 24 bytes}Assembly for the copy:
copy_version: ; Simplified. rdi = src, the only argument push rbx mov rbx, rdi ; Keep src safe across calls
; Allocate new buffer mov rdi, QWORD PTR [rbx+8] ; src.end sub rdi, QWORD PTR [rbx] ; end - begin = byte count call operator new(unsigned long) ; Allocate 4,000,000 bytes!
; Copy all data mov rdi, rax ; destination mov rsi, QWORD PTR [rbx] ; source begin mov rdx, QWORD PTR [rbx+8] sub rdx, QWORD PTR [rbx] ; byte count call memcpy ; Copy 4,000,000 bytes!
; Set up the new vector's pointers, then dst's destructor frees the copy ; ... pop rbx retAssembly for the move:
move_version: ; rdi = src, the only argument. dst never touches memory: it lives in registers mov rax, rdi mov rdi, QWORD PTR [rdi] ; dst.begin = src.begin mov rsi, QWORD PTR [rax+16] ; dst.capacity = src.capacity
; Null out source xorps xmm0, xmm0 movups XMMWORD PTR [rax], xmm0 ; src.begin = src.end = nullptr mov QWORD PTR [rax+16], 0 ; src.capacity = nullptr
; dst goes out of scope: its destructor frees the buffer it just stole test rdi, rdi je .done sub rsi, rdi ; capacity in bytes, for sized delete jmp operator delete(void*, unsigned long).done: retThat's Clang's -O2 output against libstdc++. Six instructions do the move itself, no matter the vector size; the rest is dst's destructor, because dst dies at the end of the function. The compiler doesn't even load src.end, since nothing ever reads dst.end. The copy scales with the data: one million elements means a 4MB allocation and 4MB of copying.
Here are the two control blocks before and after dst = std::move(src). Only the 24 bytes on the left change hands. The four megabytes on the right never move, and the zeroed src is why its destructor later has nothing to free.
RVO and NRVO: The Compiler Eliminates Everything
Return Value Optimisation (RVO) one-ups moves: it deletes the copy or move outright.
std::vector<int> make_vector() { std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); return v; // No copy, no move!}
void caller() { std::vector<int> result = make_vector();}With RVO (mandatory since C++17 for prvalues):
; The compiler transforms this into:; make_vector(vector<int> *__result) {; __result->push_back(1);; __result->push_back(2);; __result->push_back(3);; }
make_vector: ; rdi = pointer to caller's result variable (passed secretly) push rbx mov rbx, rdi ; Save pointer to result
; Initialize vector directly in caller's memory mov QWORD PTR [rbx], 0 ; begin = null mov QWORD PTR [rbx+8], 0 ; end = null mov QWORD PTR [rbx+16], 0 ; capacity = null
; push_back(1) - operates on caller's vector directly mov rdi, rbx mov esi, 1 call vector_push_back
; push_back(2) mov rdi, rbx mov esi, 2 call vector_push_back
; push_back(3) mov rdi, rbx mov esi, 3 call vector_push_back
pop rbx ret ; No copy or move! Result already in place.The caller smuggles the address of its result variable in as a hidden parameter. The function builds the vector straight into the caller's memory. By the time ret fires, the vector is already where it belongs. No copies, no moves, no overhead.
Named RVO (NRVO)
NRVO covers named local variables, not only temporaries. The standard doesn't mandate it, but every major compiler does it:
std::string build_message(int code) { std::string result; result = "Error "; result += std::to_string(code); return result; // NRVO: constructed in-place in caller's memory}You can verify NRVO by making the copy and move constructors announce themselves. (Grepping the assembly for "move" won't help: constructor symbols are mangled, so the word never appears.) The runner compiles as C++14, where even returning a temporary is elision the compiler is permitted to do rather than required to, and GCC does it anyway. Predict which of the five sections print COPY or move before you run it.
When Move Optimisation Fails
Moves don't always vanish. Cases where you actually pay for them:
// 1. Moving from a container elementstd::string s = std::move(vec[0]); // Must null-out vec[0]
// 2. Returning something that isn't a local objectstd::string get_one_of(bool flag, std::string &&a, std::string &&b) { if (flag) return std::move(a); return std::move(b); // a and b are references to the caller's strings, so there's nothing to // elide: the return value is move-constructed from one of them}When the move constructor really does run, you're still looking at a handful of pointer copies. Not a deep copy.
POD Types: Move Is Copy
For plain-old-data types, copy and move are the same instructions:
struct Point { double x, y, z; };
void copy_point(const Point &src) { Point dst = src; }void move_point(Point &&src) { Point dst = std::move(src); }; Both generate identical code: movsd xmm0, QWORD PTR [rsi] ; Copy x movsd QWORD PTR [rdi], xmm0 movsd xmm0, QWORD PTR [rsi+8] ; Copy y movsd QWORD PTR [rdi+8], xmm0 movsd xmm0, QWORD PTR [rsi+16] ; Copy z movsd QWORD PTR [rdi+16], xmm0A type without heap resources of its own has nothing to "move." Move semantics earn their keep on types that manage external resources — heap memory, file handles, sockets — by transferring ownership rather than duplicating the resource.
Key Takeaways
-
std::moveemits zero instructions. It's a cast that nudges overload resolution. -
Move constructors are a fistful of pointer copies. For
vectorand friends, a move is a handful of loads and stores regardless of data size.std::stringadds a branch for its small-string buffer, but it's still a short, fixed amount of work. -
RVO eats the move outright. The compiler builds the return value directly in the caller's memory. Mandatory for prvalues since C++17.
-
For POD types, move equals copy. Move semantics earn their keep on types that own resources.
-
The compiler is very good at this. Between RVO, NRVO, copy elision, and inlining, most copies you'd lose sleep over are already gone. Write clear code first; reach for explicit
std::moveonly when a profiler points at a real bottleneck.