Handling Memory Leaks in Classes

Medium Dynamic Memory & Smart Pointers
Solve in Playground →

Problem Statement

Create a `Resource` class that allocates a dynamic integer internally during construction: `data = new int(val);`. Provide a destructor that explicitly runs `delete data;` and prints ‘Cleaned’. Instantiate it in a local scope to trigger the destructor.

Input Format

A single integer.

Output Format

Print 'Data: [val]' inside the scope, then 'Cleaned' when destroyed.

Constraints

Must utilize a destructor to free memory.

Sample Input

10

Sample Output

Data: 10
Cleaned

Explanation

Classes that manage dynamic resources MUST clean up their own memory in the destructor to prevent leaks when the object falls out of scope.

Starter Code

#include <iostream>

class Resource {
public:
    int* data;
    // Write your constructor and destructor here
};

int main() {
    int val;
    if (std::cin >> val) {
        {
            // Instantiate Resource, print data
        }
    }
    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