Menu
Coddy logo textTech

Freeing Memory with 'delete'

Part of the Logic & Flow section of Coddy's C++ journey — lesson 7 of 56.

When you allocate memory dynamically using new, that memory doesn't automatically disappear when you're done with it. Unlike regular variables that are cleaned up automatically, dynamically allocated memory stays in use until you explicitly free it. This is where the delete keyword becomes essential.

The delete keyword deallocates memory that was previously allocated with new. When you use delete, you're telling the system that you're finished with that memory and it can be reused for other purposes:

int* ptr = new int(42);  // Allocate memory
// Use the memory...
delete ptr;              // Free the memory

The fundamental rule of dynamic memory management is simple: for every new, there must be a corresponding delete. If you forget to delete dynamically allocated memory, you create a memory leak - your program continues to hold onto memory it's no longer using, which can eventually cause your system to run out of available memory.

After calling delete on a pointer, that pointer becomes invalid and should not be used again. It's good practice to set the pointer to nullptr after deleting to avoid accidentally using it.

challenge icon

Challenge

Easy

Create a program that demonstrates the complete dynamic memory lifecycle by allocating memory with new, using it, and then properly deallocating it with delete.

The following inputs will be provided:

  • An integer value to store in dynamically allocated memory
  • A second integer value to update the stored value

Your program should:

  1. Use new to dynamically allocate memory for an integer and store the pointer in a variable named dynamicPtr
  2. Assign the first input value to the dynamically allocated memory using the dereference operator
  3. Print the initial value stored in the dynamically allocated memory
  4. Update the value in the dynamically allocated memory to the second input value
  5. Print the updated value
  6. Use delete to free the dynamically allocated memory
  7. Set the pointer to nullptr after deletion
  8. Print a confirmation message that the memory has been freed

Use the following exact output format:

Initial value: [first value]
Updated value: [second value]
Memory freed successfully

Cheat sheet

The delete keyword deallocates memory that was previously allocated with new:

int* ptr = new int(42);  // Allocate memory
// Use the memory...
delete ptr;              // Free the memory

For every new, there must be a corresponding delete to avoid memory leaks.

After calling delete, set the pointer to nullptr to avoid accidentally using invalid memory:

delete ptr;
ptr = nullptr;

Try it yourself

#include <iostream>
using namespace std;

int main() {
    // Read input values
    int firstValue, secondValue;
    cin >> firstValue;
    cin >> secondValue;
    
    // TODO: Write your code below
    // 1. Allocate memory using new and store in dynamicPtr
    // 2. Assign firstValue to the allocated memory
    // 3. Print initial value
    // 4. Update with secondValue
    // 5. Print updated value
    // 6. Delete the memory and set pointer to nullptr
    // 7. Print confirmation message
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow