Topic 86 of 91 11. Additional C++ Topics Added Interactive Tutorial

Move Semantics in C++: Efficient Resource Transfer With Rvalue References

7 mins read Last updated: 26 Sep 2026 19,782 views

Direct Answer & Core Concept

Move semantics allows resources (such as heap buffers or file descriptors) to be transferred directly from temporary expiring objects (rvalues) into new objects without deep copying. Rvalue references (Type&&) bind to temporary objects, and std::move casts an lvalue to an rvalue to enable move construction and assignment.

Detailed Explanation & Mechanics

1. Lvalues vs Rvalues:

  • Lvalue: Has an identifiable memory address and name (e.g. int x = 10;, x is an lvalue).
  • Rvalue: Temporary value or literal without persistent identity (e.g. 10 + 20, temporary strings).
main.cpp
Run Online
COPY vs MOVE OF HEAP BUFFER:
COPY: Allocate new buffer -> memcpy elements (Expensive $O(N)$)
MOVE: Steal source pointer (dest.ptr = src.ptr; src.ptr = nullptr;) ($O(1)$ Instantaneous!)

2. Move Constructor Signature:

MyClass(MyClass&& other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }

Runnable Code Example: Move Semantics in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <vector>
#include <utility>

class BigBuffer {
private:
    size_t size;
    int* data;

public:
    explicit BigBuffer(size_t s) : size(s), data(new int[s]) {
        std::cout << "[Allocated] BigBuffer with " << size << " ints.\n";
    }

    // Move Constructor: Steals pointer in O(1)
    BigBuffer(BigBuffer&& other) noexcept : size(other.size), data(other.data) {
        other.size = 0;
        other.data = nullptr; // Leave source in valid empty state
        std::cout << "[MOVE Constructor] Pointer stolen safely.\n";
    }

    ~BigBuffer() {
        delete[] data;
    }

    // Delete copy constructor to demonstrate pure move semantics
    BigBuffer(const BigBuffer&) = delete;
    BigBuffer& operator=(const BigBuffer&) = delete;
};

int main() {
    std::cout << "Creating initial buffer...\n";
    BigBuffer buf1(1000000);

    std::cout << "Moving buffer into vector...\n";
    std::vector<BigBuffer> container;
    container.push_back(std::move(buf1)); // Invokes Move Constructor

    std::cout << "Move completed successfully.\n";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Does `std::move` actually move any data by itself?

A: No. `std::move` is merely a compile-time static cast to an rvalue reference (`static_cast(var)`). It signals that the object may be moved from, enabling the compiler to select move constructors/assignment operators.
Q

Q: Why should move constructors and move assignment operators be marked `noexcept`?

A: STL containers like `std::vector` will only use move operations during dynamic reallocations if the move constructor is `noexcept`; otherwise, they fallback to copying for exception safety guarantees.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Always mark move constructors and move assignment operators noexcept.
• Always leave the moved-from object in a valid, destructible state (e.g., set moved raw pointers to nullptr).
• Do not access or read from an object after calling std::move on it.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.