J
Jason
Did I used NEW and DELETE operator right?
//Start
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int *aaa = new int[3];
aaa[0] = 0; aaa[1] = 1; aaa[2] = 2;
delete [] aaa;
aaa[1] = 20;
cout<<aaa[1]<<endl; //prints 20 on my screen
system("PAUSE");
return 0;
}
//End
I thought when I call the delete [] aaa , the memory allocated to aaa
is returned to the system. The memory is beyond of my program's
access. So when I write aaa[1] = 20, I should get a memory access
violation, but I don't. Why not? There's two possible explanation I
can think of: 1. "delete" only tells the OS to recycle the memory
after the program terminates, but doesn't return the memory back to
the system immediately. 2. I simply misused NEW and DELETE. Which one
is it?
//Start
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int *aaa = new int[3];
aaa[0] = 0; aaa[1] = 1; aaa[2] = 2;
delete [] aaa;
aaa[1] = 20;
cout<<aaa[1]<<endl; //prints 20 on my screen
system("PAUSE");
return 0;
}
//End
I thought when I call the delete [] aaa , the memory allocated to aaa
is returned to the system. The memory is beyond of my program's
access. So when I write aaa[1] = 20, I should get a memory access
violation, but I don't. Why not? There's two possible explanation I
can think of: 1. "delete" only tells the OS to recycle the memory
after the program terminates, but doesn't return the memory back to
the system immediately. 2. I simply misused NEW and DELETE. Which one
is it?