NEW and DELETE operator question

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?
 
J

Jonathan Turkanis

Jason said:
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?

You misused delete. After deleting aaa, accessing aaa[1] is undefined.
The standard doesn't require an access violation. Your implementation
is free to wait as long as it likes before reusing the given memory.

Jonathan

Jonathan
 
J

John Harrison

gabriel said:
You misused it.

No, he didn't misuse new or delete, the misuse came later.

To the OP, the standard describes programs like the above as 'undefined
behaviour' which means anything could happen. An access violation is not
required, only possible.

john
 
G

gabriel

John said:
No, he didn't misuse new or delete, the misuse came later.

Jeez, talk about anal retentive! The point was that the OP shouldn't have
done that in his code. From the way OP asked the question, you know he got
the point.
 
J

John Harrison

gabriel said:
Jeez, talk about anal retentive! The point was that the OP shouldn't have
done that in his code. From the way OP asked the question, you know he got
the point.

I read the OP question as specifically asking 'Why did my not crash, did I
misuse new or delete?' I thought you answer was potentially confusing
because he might have understood you as meaning he did misuse new or delete.

john
 
J

Jonathan Turkanis

I read the OP question as specifically asking 'Why did my not crash, did I
misuse new or delete?' I thought you answer was potentially confusing
because he might have understood you as meaning he did misuse new or delete.

I think deleting an object before your through using it could be
characterized as 'misusing delete'.

Jonathan
 
J

John Harrison

Jonathan Turkanis said:
I think deleting an object before your through using it could be
characterized as 'misusing delete'.

Jonathan

Not trying to split hairs, just trying to help the OP, who's probably bored
with the subject now.

John
 
J

John Carson

John Harrison said:
Not trying to split hairs, just trying to help the OP, who's probably
bored with the subject now.

John


I think that your initial post was completely appropriate.

The OP clearly knew that deleting an object when he wasn't finished with it
was wrong --- so wrong that he expected an access violation. Thus his
question about whether he "misused delete" could not have been referring to
that particular misuse. In fact, he was seeking an explanation for the
*absence* of an access violation. And the *absence* of an access violation
was not because he misused delete.
 
J

Jonathan Turkanis

I think that your initial post was completely appropriate.

The OP clearly knew that deleting an object when he wasn't finished with it
was wrong --- so wrong that he expected an access violation. Thus his
question about whether he "misused delete" could not have been referring to
that particular misuse. In fact, he was seeking an explanation for the
*absence* of an access violation. And the *absence* of an access violation
was not because he misused delete.

I see now. The OP suspected that since there was no access violation
he might not have deleted the pointer properly. For instance, maybe
you have to use delete twice to get the full effect, or use boldface
;-) This didn't occur to me.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top