Bird
Raised Fist0
C++programming~10 mins

new operator in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - new operator
Program starts
↓
Call new operator
↓
Allocate memory on heap
↓
Return pointer to memory
↓
Use pointer to access memory
↓
Delete pointer to free memory
↓
Program ends
The new operator allocates memory on the heap and returns a pointer to it, which you can use until you delete it to free memory.
Execution Sample
C++
int* p = new int(10);
std::cout << *p << std::endl;
delete p;
This code allocates an int with value 10 on the heap, prints it, then frees the memory.
Execution Table
StepActionMemory AllocationPointer ValueOutput
1Call new int(10)Allocates int with value 10 on heapPointer p points to new int
2Dereference pointer *pNo new allocationPointer p unchanged10
3Delete pointer pMemory freedPointer p becomes dangling (not safe to use)
4End programNo allocationPointer p is dangling
💡 Program ends after memory is freed with delete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
puninitializedpoints to heap int(10)points to heap int(10)dangling pointerdangling pointer
Key Moments - 3 Insights
Why do we need to use delete after new?
Because new allocates memory on the heap that stays until deleted. Without delete, memory stays allocated causing leaks (see step 3 in execution_table).
What happens if we try to use pointer p after delete?
Pointer p becomes dangling after delete (step 3). Using it after delete can cause errors or crashes.
Does new initialize the allocated memory?
Yes, in this example new int(10) initializes the int to 10 (step 1). Without (10), the value would be undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 2?
A10
BPointer address
CUndefined
D0
💡 Hint
Check the Output column at step 2 in execution_table
At which step does the pointer become unsafe to use?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Pointer Value column in execution_table for step 3
If we omit delete, what changes in the execution_table?
AMemory is freed automatically
BProgram crashes immediately
CMemory is never freed, pointer stays valid
DPointer becomes dangling earlier
💡 Hint
Refer to the Memory Allocation and Pointer Value columns in execution_table
Concept Snapshot
new operator allocates memory on the heap and returns a pointer.
Use new Type(value) to create and initialize.
Always pair new with delete to free memory.
Pointer holds address of allocated memory.
Dereference pointer to access value.
Using pointer after delete is unsafe.
Full Transcript
The new operator in C++ allocates memory on the heap and returns a pointer to that memory. For example, int* p = new int(10); creates an integer with value 10 on the heap and p points to it. You can access the value by dereferencing p with *p. After using the memory, you must call delete p; to free it. If you don't delete, the memory stays allocated causing a memory leak. After delete, the pointer becomes dangling and should not be used. This trace shows each step: allocation, access, deletion, and program end.

Practice

(1/5)
1. What does the new operator do in C++?
easy
A. Allocates memory dynamically and returns a pointer to it
B. Deletes a variable from memory
C. Declares a normal variable on the stack
D. Copies data from one variable to another

Solution

  1. Step 1: Understand the purpose of new

    The new operator is used to allocate memory on the heap during program execution.
  2. Step 2: Identify what new returns

    It returns a pointer to the allocated memory so the program can use it.
  3. Final Answer:

    Allocates memory dynamically and returns a pointer to it -> Option A
  4. Quick Check:

    new = allocate memory [OK]
Hint: Remember: new creates memory and gives a pointer [OK]
Common Mistakes:
  • Confusing new with delete
  • Thinking new declares stack variables
  • Assuming new copies data
2. Which of the following is the correct syntax to allocate an integer using new?
easy
A. int p = new int;
B. int p = new int();
C. int *p = new int[];
D. int *p = new int;

Solution

  1. Step 1: Check pointer declaration

    Using new returns a pointer, so the variable must be a pointer type like int *.
  2. Step 2: Verify syntax correctness

    int *p = new int; correctly allocates an int and assigns its address to p.
  3. Final Answer:

    int *p = new int; -> Option D
  4. Quick Check:

    Pointer = new type [OK]
Hint: new returns pointer, so use pointer variable [OK]
Common Mistakes:
  • Declaring variable without pointer
  • Using parentheses incorrectly
  • Assigning new to non-pointer variable
3. What is the output of this code?
int *arr = new int[3]{10, 20, 30};
std::cout << arr[1] << std::endl;
delete[] arr;
medium
A. 20
B. 10
C. 30
D. Runtime error

Solution

  1. Step 1: Understand array allocation with new

    The code creates an array of 3 integers with values 10, 20, 30.
  2. Step 2: Access the second element

    Array indexing starts at 0, so arr[1] is the second element, which is 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    arr[1] = 20 [OK]
Hint: Remember array index starts at 0 [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Forgetting to use delete[] for arrays
  • Expecting runtime error without cause
4. Identify the error in this code snippet:
int *p = new int;
*p = 5;
delete p;
delete p;
medium
A. Incorrect use of new operator
B. Missing initialization of pointer
C. Double deletion of the same pointer
D. No error, code is correct

Solution

  1. Step 1: Analyze pointer allocation and deletion

    Pointer p is allocated memory and deleted once correctly.
  2. Step 2: Check for repeated delete

    Deleting p a second time causes undefined behavior (double deletion error).
  3. Final Answer:

    Double deletion of the same pointer -> Option C
  4. Quick Check:

    delete twice = error [OK]
Hint: Delete pointer only once to avoid errors [OK]
Common Mistakes:
  • Deleting pointer twice
  • Not deleting allocated memory
  • Confusing delete and delete[]
5. You want to create a dynamic 2D array of integers with 3 rows and 2 columns using new. Which code correctly allocates and deallocates this array?
hard
A. int *arr = new int[3*2]; // use arr delete arr;
B. int **arr = new int*[3]; for(int i=0; i<3; i++) arr[i] = new int[2]; // use arr for(int i=0; i<3; i++) delete[] arr[i]; delete[] arr;
C. int **arr = new int[3][2]; // use arr delete[] arr;
D. int arr[3][2]; // use arr // no delete needed

Solution

  1. Step 1: Allocate 2D array dynamically

    int **arr = new int*[3]; for(int i=0; i<3; i++) arr[i] = new int[2]; // use arr for(int i=0; i<3; i++) delete[] arr[i]; delete[] arr; correctly creates an array of pointers for rows, then allocates each row as an array of ints.
  2. Step 2: Properly deallocate memory

    Each row is deleted with delete[], then the array of pointers is deleted with delete[].
  3. Final Answer:

    Uses an array of pointers, allocating each row separately -> Option B
  4. Quick Check:

    Nested new and delete[] for 2D arrays [OK]
Hint: Use nested new and delete[] for 2D arrays [OK]
Common Mistakes:
  • Using delete instead of delete[] for arrays
  • Not deleting all allocated rows
  • Trying to allocate 2D array with single new incorrectly
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