Menu
Coddy logo textTech

Dynamic Memory with 'new'

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

So far, you've worked with pointers that point to variables already created in your program. But what if you need to create variables while your program is running? This is where dynamic memory allocation with the new keyword becomes essential.

The new keyword allows you to allocate memory for a variable on the heap during program execution. Unlike regular variables that are created on the stack, dynamically allocated memory persists until you explicitly free it.

int* ptr = new int;  // Allocates memory for an integer
*ptr = 42;           // Assigns a value to the allocated memory

In this example, new int allocates enough memory to store an integer and returns a pointer to that memory location. You can then use the dereference operator to assign values and access the dynamically allocated variable just like any other pointer.

Dynamic memory allocation is particularly useful when you don't know how much memory you'll need until runtime, or when you need variables to exist beyond the scope where they were created. This flexibility makes new a powerful tool for building more complex programs.

challenge icon

Challenge

Easy

Create a program that demonstrates dynamic memory allocation using the new keyword to allocate memory for a variable during program execution.

The following input will be provided:

  • An integer value to store in the dynamically allocated memory

Your program should:

  1. Use the new keyword to dynamically allocate memory for an integer
  2. Store the pointer returned by new in a pointer variable named numPtr
  3. Use the dereference operator to assign the input value to the dynamically allocated memory
  4. Print the value stored in the dynamically allocated memory by dereferencing the pointer
  5. Print the memory address stored in the pointer

Use the following exact output format:

Value: [value]
Address: [memory address]

The memory address will be displayed in hexadecimal format, which is how C++ shows memory addresses for dynamically allocated memory.

Cheat sheet

The new keyword allocates memory for a variable on the heap during program execution:

int* ptr = new int;  // Allocates memory for an integer
*ptr = 42;           // Assigns a value to the allocated memory

Unlike regular variables created on the stack, dynamically allocated memory persists until explicitly freed and can exist beyond the scope where it was created.

Try it yourself

#include <iostream>
using namespace std;

int main() {
    // Read input
    int value;
    cin >> value;
    
    // TODO: Write your code below
    // Use 'new' to dynamically allocate memory for an integer
    // Store the pointer in a variable named 'numPtr'
    // Assign the input value to the dynamically allocated memory
    
    // Output the result
    cout << "Value: " << /* dereference numPtr here */ << endl;
    cout << "Address: " << /* print the address stored in numPtr */ << endl;
    
    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