/* * Program to look at dangling pointer error */ #include #include using namespace std; int main() { int *ip, *ip2; // dynamically assign memory ip = new int; *ip = 268; printf("Initial values: ip=%u, *ip=%d\n", ip, *ip); // effect of delete on pointers delete ip; // should never reference pointer after executing delete !!! printf("Values after delete: ip=%u, *ip=%d\n", ip, *ip); *ip = 100; printf("Deference of dangling pointer: ip=%u, *ip=%d\n", ip, *ip); // next call to 'new' re-allocates same memory space. But, this behavior // depends on the runtime system, and can change! ip2 = new int; *ip2 = 233; printf("Second call to new: ip2=%u, *ip2=%d, (ip=%u, *ip=%d)\n", ip2, *ip2, ip, *ip2); return 0; }