Bird
Raised Fist0
C++programming~20 mins

Memory leak concept in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C++ code snippet?

Consider the following C++ code. What will it print?

C++
#include <iostream>

int main() {
    int* ptr = new int(10);
    ptr = new int(20);
    std::cout << *ptr << std::endl;
    return 0;
}
A10
B20
CCompilation error
DUndefined behavior (possible crash)
Attempts:
2 left
💡 Hint

Think about what happens to the first allocated memory when the pointer is reassigned.

❓ Predict Output
intermediate
2:00remaining
What error does this code cause?

Analyze this C++ code snippet. What error will it cause when run?

C++
#include <iostream>

int* createArray() {
    int arr[5] = {1,2,3,4,5};
    return arr;
}

int main() {
    int* p = createArray();
    std::cout << p[0] << std::endl;
    return 0;
}
ANo error, prints 1
BCompilation error: returning address of local variable
CRuntime error: segmentation fault
DUndefined behavior: accessing invalid memory
Attempts:
2 left
💡 Hint

Think about the lifetime of local variables and what happens when you return their address.

🔧 Debug
advanced
2:00remaining
Identify the memory leak in this code

Which line causes a memory leak in the following C++ code?

C++
#include <iostream>

void process() {
    int* data = new int[10];
    for (int i = 0; i < 10; ++i) {
        data[i] = i * 2;
    }
    if (data[0] < 5) {
        data = new int[5];
    }
    delete[] data;
}

int main() {
    process();
    return 0;
}
ALine: data = new int[5];
BLine: int* data = new int[10];
CLine: delete[] data;
DNo memory leak
Attempts:
2 left
💡 Hint

Consider what happens to the first allocated array when data is reassigned.

📝 Syntax
advanced
2:00remaining
Which option causes undefined behavior?

Which of the following C++ code snippets will cause undefined behavior related to memory management?

Aint* p = new int[5]; delete p;
Bint* p = new int[5]; delete[] p;
Cint* p = new int(5); delete p;
Dint* p = nullptr; delete p;
Attempts:
2 left
💡 Hint

Think about the correct way to delete memory allocated with new[].

🚀 Application
expert
3:00remaining
How many memory leaks occur in this program?

Analyze the following C++ program. How many memory leaks occur when it runs?

C++
#include <iostream>

class Node {
public:
    int value;
    Node* next;
    Node(int v) : value(v), next(nullptr) {}
};

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);

    Node* temp = head;
    while (temp != nullptr) {
        temp = temp->next;
    }

    // No delete calls
    return 0;
}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Count how many new allocations are made and how many delete calls are missing.

Practice

(1/5)
1.

What is a memory leak in C++?

easy
A. When variables are declared but not used
B. When a program runs too fast
C. When allocated memory is not released back to the system
D. When the program uses too many functions

Solution

  1. Step 1: Understand memory allocation

    Memory allocated with new must be freed with delete.
  2. Step 2: Define memory leak

    If memory is allocated but not freed, it stays reserved and causes a leak.
  3. Final Answer:

    When allocated memory is not released back to the system -> Option C
  4. Quick Check:

    Memory leak = not freeing allocated memory [OK]
Hint: Memory leak means forgetting to free allocated memory [OK]
Common Mistakes:
  • Confusing memory leak with slow program
  • Thinking unused variables cause leaks
  • Mixing memory leak with logical errors
2.

Which of the following C++ code snippets correctly frees allocated memory?

int* ptr = new int(5);
// What should be done next?
easy
A. free(ptr);
B. delete ptr;
C. ptr = nullptr;
D. ptr = new int(10);

Solution

  1. Step 1: Identify allocation method

    Memory is allocated with new, so it must be freed with delete.
  2. Step 2: Match correct freeing syntax

    free() is for malloc(), not new. Setting pointer to nullptr does not free memory.
  3. Final Answer:

    delete ptr; -> Option B
  4. Quick Check:

    Use delete for new, free for malloc [OK]
Hint: Use delete for new, not free [OK]
Common Mistakes:
  • Using free() with new
  • Just setting pointer to nullptr without delete
  • Allocating new memory without deleting old
3.

What is the output of this C++ code?

int* ptr = new int(10);
ptr = new int(20);
std::cout << *ptr << std::endl;
medium
A. 10
B. Compilation error
C. Undefined behavior
D. 20

Solution

  1. Step 1: Analyze pointer reassignment

    First, ptr points to 10. Then ptr is reassigned to new int(20) without deleting old memory.
  2. Step 2: Determine output

    Pointer now points to 20, so output is 20. The old memory is leaked but does not affect output.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Pointer points to last new value = 20 [OK]
Hint: Pointer shows last assigned value, old memory leaks [OK]
Common Mistakes:
  • Expecting output 10 after reassignment
  • Thinking code causes compile error
  • Confusing memory leak with runtime error
4.

Find the memory leak in this code and fix it:

void func() {
  int* arr = new int[5];
  arr = new int[10];
  delete[] arr;
}
medium
A. Delete first array before reassigning arr
B. Use delete instead of delete[]
C. No leak, code is correct
D. Change new int[10] to new int(10)

Solution

  1. Step 1: Identify memory allocations

    First, arr points to array of 5 ints, then reassigned to array of 10 ints without deleting first.
  2. Step 2: Fix memory leak

    Delete the first array before reassigning arr to avoid leak. Use delete[] for arrays.
  3. Final Answer:

    Delete first array before reassigning arr -> Option A
  4. Quick Check:

    Delete old array before new assignment [OK]
Hint: Delete old array before assigning new to pointer [OK]
Common Mistakes:
  • Not deleting first allocated array
  • Using delete instead of delete[] for arrays
  • Ignoring memory leak warnings
5.

You have a class that allocates memory in its constructor using new. Which approach best prevents memory leaks?

hard
A. Use smart pointers like std::unique_ptr to manage memory automatically
B. Manually call delete in destructor and follow Rule of Three
C. Use malloc and free instead of new and delete
D. Never use new, only stack variables

Solution

  1. Step 1: Understand manual memory management risks

    Manually calling delete and following Rule of Three is error-prone and can cause leaks if forgotten.
  2. Step 2: Use smart pointers for safety

    Smart pointers automatically free memory when no longer used, preventing leaks safely and easily.
  3. Final Answer:

    Use smart pointers like std::unique_ptr to manage memory automatically -> Option A
  4. Quick Check:

    Smart pointers prevent leaks automatically [OK]
Hint: Use smart pointers to avoid manual delete [OK]
Common Mistakes:
  • Relying only on manual delete
  • Avoiding new but not managing memory
  • Mixing malloc/free with new/delete
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard