A Few Questions Concerning New and Delete.
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>
2.) main()
3.) {
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;
9.) }
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.:p
Re: A Few Questions Concerning New and Delete.
Quote:
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>
2.) main()
3.) {
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;
9.) }
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.:p
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.
to modify or display the int that a points to instead of the address, you need to derefrence it:
Code:
int *a;
a = new int;
cout << a; // address
*a = 55; // number
cout << *a; // number
delete a;
Re: A Few Questions Concerning New and Delete.
Quote:
Originally posted by LordVirusXXP
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?
As far as I know that's right.