0
0
C++programming~10 mins

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

Choose your learning style9 modes available
Concept Flow - delete operator
Allocate memory with new
Use pointer to access memory
Call delete on pointer
Memory freed
Pointer becomes dangling (unsafe)
Set pointer to nullptr to avoid dangling
This flow shows how memory is allocated with new, used, then freed with delete, and why setting pointer to nullptr is important.
Execution Sample
C++
int* p = new int(10);
std::cout << *p << std::endl;
delete p;
p = nullptr;
Allocates an int with value 10, prints it, deletes the memory, then sets pointer to nullptr.
Execution Table
StepActionPointer pMemory StateOutput
1Allocate int with value 10p -> address1Memory at address1: 10
2Print value pointed by pp -> address1Memory at address1: 1010
3Delete memory pointed by pp -> address1 (dangling)Memory at address1: freed
4Set p to nullptrp = nullptrNo allocated memory
💡 Pointer p set to nullptr to avoid dangling pointer after delete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
puninitializedaddress1address1address1 (dangling)nullptr
Key Moments - 2 Insights
Why do we set the pointer to nullptr after delete?
After delete, the pointer still holds the old address but that memory is freed, making it unsafe (dangling). Setting it to nullptr avoids accidental use, as shown in step 4 of the execution_table.
What happens if we try to access *p after delete but before setting nullptr?
Accessing *p after delete leads to undefined behavior because the memory is freed (step 3). The pointer is dangling and does not point to valid memory.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of pointer p after step 3?
Ap is a dangling pointer pointing to freed memory
Bp is nullptr
Cp points to valid memory with value 10
Dp is uninitialized
💡 Hint
Check the 'Pointer p' and 'Memory State' columns in row for step 3
At which step is the value 10 printed to the output?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table
If we skip setting p to nullptr after delete, what risk do we face?
AMemory leak
BDangling pointer usage
CCompilation error
DNo risk, safe to use
💡 Hint
Refer to key_moments explanation about pointer safety after delete
Concept Snapshot
delete operator in C++:
- Used to free memory allocated with new
- Syntax: delete pointer;
- After delete, pointer becomes dangling
- Set pointer to nullptr to avoid unsafe access
- Accessing deleted memory causes undefined behavior
Full Transcript
This visual trace shows how the delete operator works in C++. First, memory is allocated with new and pointer p points to it. Then, the value is printed. Next, delete frees the memory but p still points to the old address, making it a dangling pointer. Finally, setting p to nullptr prevents accidental use of freed memory. Key points include always setting pointers to nullptr after delete and never accessing memory after it is deleted.