hi
a variable is allocated in dynamic memory(using new and is not deleted) and the programs gets terminated .now does the memory allocated will become junk.
int main()
{
int *i = new int;
}
int main()
{
int *i = new int;
}
To copy to clipboard, switch view to plain text mode
if the above program is executed it just allocates 4 bytes in heap and the program gets terminated.
does this allocated memory will never be free even after program termination.because the books say that the newed variables can made free only with delete.
also,
if a variable is newed twice then will the memory be allocated twice.
#include <iostream.h>
int main()
{
int *i;
i = new int; //newed first time
cout<<&*i<<" ";
i= new int; //newed second time
cout<<&*i;
}
#include <iostream.h>
int main()
{
int *i;
i = new int; //newed first time
cout<<&*i<<" ";
i= new int; //newed second time
cout<<&*i;
}
To copy to clipboard, switch view to plain text mode
in the above code the variable i is newed twice and the address of newed location is different on both allocations.
how to find a variable is already newed or not.
Bookmarks