NEW和DELETE运算符问题 [英] NEW and DELETE operator question

查看:83
本文介绍了NEW和DELETE运算符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否使用了NEW和DELETE运算符?


//开始

#include< iostream>

使用namespace std;


int main(int argc,char * argv [])

{

int * aaa = new int [3];


aaa [0] = 0; aaa [1] = 1; aaa [2] = 2;


删除[] aaa;

aaa [1] = 20;

cout<< ; AAA [1];< ENDL; //在我的屏幕上打印20


system(PAUSE);

返回0;

}

//结束


我以为当我调用delete [] aaa时,分配给aaa

的内存将返回给系统。内存超出了我的程序'

访问权限。所以当我写aaa [1] = 20时,我应该获得内存访问

违规,但我不是。为什么不?有两种可能的解释我可以想到:
可以想到:1。删除只是告诉操作系统在程序终止后回收内存

,但是不会立即将内存返回到系统的
。我只是误用了NEW和DELETE。哪一个

是吗?

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?

推荐答案

Jason写道:
Jason wrote:
系统立即。我只是误用了NEW和DELETE。它是哪一个?
the system immediately. 2. I simply misused NEW and DELETE. Which one
is it?




你误用了它。


-

gabriel



You misused it.

--
gabriel




" Jason" < NI ******** @ yahoo.com>在消息中写道

news:de ************************** @ posting.google.c om ...

"Jason" <ni********@yahoo.com> wrote in message
news:de**************************@posting.google.c om...
我使用了NEW和DELETE运算符吗?
//
#include< iostream>
使用命名空间std;

int main(int argc,char * argv [])
{* / int * aaa = new int [3];

aaa [0] = 0; aaa [1] = 1; aaa [2] = 2;

删除[] aaa;
aaa [1] = 20;
cout<< aaa [1]<< endl; //在我的屏幕上打印20

系统(暂停);
返回0;
}
//结束

我想当我调用delete [] aaa时,分配给
aaa的内存将返回给系统。内存超出了我程序的访问权限。所以当我写aaa [1] = 20时,我应该得到一次内存访问违规,但我不是。为什么不?我可以想到两种可能的解释:1。删除程序终止后,只告诉操作系统回收内存,但不会立即将内存返回系统。我只是误用了NEW和DELETE。哪一个是
一个?
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?




您误用了删除。删除aaa后,访问aaa [1]未定义。

标准不需要访问冲突。您的实施

可以在重复使用给定内存之前随意等待。


Jonathan


Jonathan



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




" gabriel" < no@no--spam.com>在消息中写道

news:43 *************************** @ msgid.meganewss ervers.com .. 。

"gabriel" <no@no--spam.com> wrote in message
news:43***************************@msgid.meganewss ervers.com...
Jason写道:
Jason wrote:
系统立即生效。我只是误用了NEW和DELETE。它是哪一个?
the system immediately. 2. I simply misused NEW and DELETE. Which one
is it?



你滥用了它。

-
gabriel



You misused it.

--
gabriel




不,他没有误用新的或删除,后来滥用了。


对于OP,标准将上述程序描述为''undefined

行为''这意味着任何事情都可能发生。违规行为不是

,只有可能。


john



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


这篇关于NEW和DELETE运算符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆