example4_double_delete_and_undefined_behavior.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>

int main() {
    int* ptr = new int(10);  // Allocate memory for an integer

    delete ptr;  // Deallocate the memory

    // Deleting the same pointer again leads to undefined behavior
    // delete ptr;  // Uncommenting this line could cause a crash

    return 0;
}
Back to delete