when you delete a;, you mark the memory it had as available. it doesnt change the value of a itself though. it's your responsibility to ensure you don't use it after deletion.
Originally posted by LordVirusXXP
This is different from my last thread concerning new and delete. I have the proper syntax, and the program compiles perfectly fine, but even after I 'delete' my new intenger, it still works when I print it to the screen.
1.) #include <iostream.h>
4.) int *a = new int; //Creates New Intenger 'a'
5.) cout << a;
6.) delete a;//Deletes Intenger 'a
7.) cout << a;//I thought this wouldn't work, but it did.
8.) return 0;
Is the reason for this because 'delete' doesn't actually delete the new intenger, but it actually de-allocates the memory that was allocated for it?
Another thing: Printing 'a' to the screen only displays it's memory address. How do I create a new intenger that can be printed to the screen with it's value, rather than it's address?
Thanks for your time.
to modify or display the int that a points to instead of the address, you need to derefrence it:
a = new int;
cout << a; // address
*a = 55; // number
cout << *a; // number