0
0
C++programming~10 mins

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

Choose your learning style9 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.