Move Constructor

Medium Move Semantics
Solve in Playground →

Problem Statement

Write a C++ program implementing a custom `Buffer` class with a move constructor to transfer ownership of dynamic heap resources efficiently.

Input Format

A single word string S.

Output Format

Print moved buffer string content.

Constraints

1 <= Length <= 50

Sample Input

MoveConstructor

Sample Output

MoveConstructor

Explanation

Move constructors steal pointers from temporaries, avoiding expensive memory reallocations and copies.

Starter Code

#include <iostream>
#include <cstring>
#include <string>

class Buffer {
private:
    char *data;

public:
    Buffer(const char *str) {
        data = new char[std::strlen(str) + 1];
        std::strcpy(data, str);
    }

    // Move Constructor
    Buffer(Buffer &&other) noexcept {
        // Write your move constructor logic here
    }

    ~Buffer() {
        delete[] data;
    }

    const char* c_str() const { return data ? data : ""; }
};

int main() {
    std::string s;
    if (std::cin >> s) {
        Buffer b1(s.c_str());
        Buffer b2(std::move(b1));
        std::cout << b2.c_str() << "n";
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used