删除相同的内存位置一段时间后取消引用内存位置 [英] dereferencing memory location some time after deleting the same one

查看:77
本文介绍了删除相同的内存位置一段时间后取消引用内存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




您如何看待以下内容?为什么我们允许这样做?

为什么C ++库并没有阻止有人愿意支持那个

assignement(* a = 20)?

#include< iostream>


使用std :: cout;


int main()

{

int * a = new int(10);

cout<< * a<< " " << a<< ''\ n'';

删除a;

* a = 20;

cout<< * a<< " " << a<< ''\\ n'';

}

编译完成后(在Linux上使用gcc-3.4.1,在XP上使用VC ++):


#。/ a.out

10 0x9f6d008

20 0x9f6d008

也许更糟糕的是,如果你编译并运行上面写的

代码而没有* a = 20;行;你得到以下输出:


#。/ a.out

10 0x9f6d008

0 0x9f6d008


我不知道这是什么行为,直到我读了一篇关于

it.comp.lang.c ++的帖子来自一个不明白为什么他得到了

打印了具有0的bin-tree的所有节点的列表。在一些先前删除的节点的

位置输出。所以他没有意识到

他忘了给父指针字段分配NULL来解决刚刚删除的节点的

(不幸的是,所有删除的节点都是leaf

)。


我认为如果他遇到了崩溃,取消引用指向

释放内存的指针,他就会理解他的

取消算法有什么不好。


如果某个程序在有人试图获得$ b时崩溃,那不是更好吗$ b取消引用指向已删除内存位置的指针?

Ciao,


Fabio De Francesco

Hi

what do you think of the following? Why are we permitted to do that?
And why the C++ Library doesn''t stop someone willing to perfom that
assignement (*a = 20)?
#include <iostream>

using std::cout;

int main()
{
int *a = new int(10);
cout << *a << " " << a << ''\n'';
delete a;
*a = 20;
cout << *a << " " << a << ''\n'';
}
After compiling (with gcc-3.4.1 on Linux and with VC++ on XP as well):

#./a.out
10 0x9f6d008
20 0x9f6d008
Maybe what is worse is that if you compile and run the above written
code without the line "*a = 20;" you get the following output:

#./a.out
10 0x9f6d008
0 0x9f6d008

I didn''t know that yhis is the behaviour until I read a post on
it.comp.lang.c++ from someone who didn''t understand why He got the
printed list of all the nodes of a bin-tree with a "0" output at the
position of some previously deleted nodes. So He didn''t realize that
He forgot to assign NULL to the parent pointer field addressing the
just deleted nodes (and unfortunatelly all the deleted nodes were leaf
ones).

I think that if He had got a crash, dereferencing a pointer to
deallocated memory, He would had understood what was bad with his
cancelling algorithm.

So wouldn''t it be better if a program crashed when someone tried to
dereference a pointer to deleted memory location?

Ciao,

Fabio De Francesco

推荐答案

fabio de francesco写道:
fabio de francesco wrote:
您如何看待以下内容?为什么我们允许这样做?


为什么不呢?标准表示任何试图在指针被删除后试图取消引用
a指针的程序都有未定义的行为。

试图定义在这种情况下应该发生的事情是浪费时间。

为什么C ++库并没有阻止有人愿意表现出来(* a = 20)?


如何停止你呢?

#include< iostream>

使用std :: cout;

int main()
{
int * a = new int(10);
cout<< * a<< " " << a<< ''\ n'';
删除a;
* a = 20;
cout<< * a<< " " << a<< ''\ n'';
}

编译完成后(在Linux上使用gcc-3.4.1,在XP上使用VC ++):

# ./a.out
10 0x9f6d008
20 0x9f6d008


未定义的行为。任何事情都可以发生。


可能更糟糕的是,如果你编译并运行上面写的代码而没有行* a = 20;你得到以下输出:

#。/ a.out
10 0x9f6d008
0 0x9f6d008


再次,它真的没有显示。该代码的行为是未定义的
。它允许产生_any_输出或没有输出



我不知道这是什么行为,直到我读了它的帖子
它.comp.lang.c ++来自一个不明白为什么他得到了带有0的bin-tree的所有节点的打印列表的人。在一些先前删除的节点的
位置输出。所以他没有意识到
他忘了给父指针字段分配NULL来解决刚刚删除的节点(不幸的是所有被删除的节点都是叶子的)。


好​​吧

我认为,如果他遇到了崩溃,取消引用指向取消分配内存的指针,他就会明白什么是坏的他的取消算法。


他可能会崩溃。或者他可以通过邮件收到感谢信。

或者他可以将他的硬盘格式化。任何事情都可以发生

发生。

当有人试图取消引用删除内存位置的指针时程序崩溃会不会更好?
what do you think of the following? Why are we permitted to do that?
Why not? The Standard says that any program that attempts to dereference
a pointer after the pointer has been deleted, has undefined behaviour.
An attempt to define what should happen in such case is a waste of time.
And why the C++ Library doesn''t stop someone willing to perfom that
assignement (*a = 20)?
How would it "stop" you?


#include <iostream>

using std::cout;

int main()
{
int *a = new int(10);
cout << *a << " " << a << ''\n'';
delete a;
*a = 20;
cout << *a << " " << a << ''\n'';
}
After compiling (with gcc-3.4.1 on Linux and with VC++ on XP as well):

#./a.out
10 0x9f6d008
20 0x9f6d008
Undefined behaviour. Anything is allowed to happen.


Maybe what is worse is that if you compile and run the above written
code without the line "*a = 20;" you get the following output:

#./a.out
10 0x9f6d008
0 0x9f6d008
Again, it really shows nothing. The behaviour of that code is
not defined. It is allowed to produce _any_ output or no output
whatsoever.

I didn''t know that yhis is the behaviour until I read a post on
it.comp.lang.c++ from someone who didn''t understand why He got the
printed list of all the nodes of a bin-tree with a "0" output at the
position of some previously deleted nodes. So He didn''t realize that
He forgot to assign NULL to the parent pointer field addressing the
just deleted nodes (and unfortunatelly all the deleted nodes were leaf
ones).
OK

I think that if He had got a crash, dereferencing a pointer to
deallocated memory, He would had understood what was bad with his
cancelling algorithm.
He could get a crash. Or he could get a thank-you note in the mail.
Or he could get his hard drive formatted. Anything is allowed to
happen.

So wouldn''t it be better if a program crashed when someone tried to
dereference a pointer to deleted memory location?




不,它不会。强制程序崩溃需要一些

特殊处理。此外,要求程序崩溃只允许为崩溃系统创建C ++程序。是定义的
。如果系统_cannot(或不能)崩溃怎么办?即使

调试这样的系统也会有问题。


V



No, it wouldn''t. Forcing the program to crash would require some
special processing. Besides, requiring the program to crash would
only permit creation of C++ programs for systems where "crash" is
defined. What if the system _cannot_ (or must not) crash? Even
debugging such system would be a problem.

V


fabio de francesco写道:
fabio de francesco wrote:
您好

您如何看待以下内容?为什么我们允许这样做?


是什么让你觉得你是谁?

为什么C ++图书馆并没有阻止有人愿意表现出来(* a) = 20)?


因为行为未定义,这意味着程序不需要

崩溃。

#include< iostream>

使用std :: cout;

int main()
{* / int * a = new int(10);
cout << * a<< " " << a<< ''\ n'';
删除a;
* a = 20;
cout<< * a<< " " << a<< ''\ n'';
}

编译完成后(在Linux上使用gcc-3.4.1,在XP上使用VC ++):

# ./a.out
10 0x9f6d008
20 0x9f6d008

也许更糟糕的是,如果你编译并运行上面写的代码而没有行* a = 20;你得到以下输出:

#。/ a.out
10 0x9f6d008
0 0x9f6d008

我不知道你是谁行为直到我读了一篇关于
it.comp.lang.c ++的帖子,其中有人不明白为什么他得到了带有0的bin-tree的所有节点的打印列表。 ;在一些先前删除的节点的
位置输出。所以他没有意识到
他忘了给父指针字段分配NULL来解决刚刚删除的节点(不幸的是所有删除的节点都是叶子的)。

我认为,如果他遇到了崩溃,取消引用指向取消分配内存的指针,他就会理解他的取消算法的坏处。

所以当有人试图取消引用删除内存位置的指针时程序崩溃会不会更好?
Hi

what do you think of the following? Why are we permitted to do that?
What makes you think you are?
And why the C++ Library doesn''t stop someone willing to perfom that
assignement (*a = 20)?
Because the behavior is undefined, which means the program is not required
to crash.
#include <iostream>

using std::cout;

int main()
{
int *a = new int(10);
cout << *a << " " << a << ''\n'';
delete a;
*a = 20;
cout << *a << " " << a << ''\n'';
}
After compiling (with gcc-3.4.1 on Linux and with VC++ on XP as well):

#./a.out
10 0x9f6d008
20 0x9f6d008
Maybe what is worse is that if you compile and run the above written
code without the line "*a = 20;" you get the following output:

#./a.out
10 0x9f6d008
0 0x9f6d008

I didn''t know that yhis is the behaviour until I read a post on
it.comp.lang.c++ from someone who didn''t understand why He got the
printed list of all the nodes of a bin-tree with a "0" output at the
position of some previously deleted nodes. So He didn''t realize that
He forgot to assign NULL to the parent pointer field addressing the
just deleted nodes (and unfortunatelly all the deleted nodes were leaf
ones).

I think that if He had got a crash, dereferencing a pointer to
deallocated memory, He would had understood what was bad with his
cancelling algorithm.

So wouldn''t it be better if a program crashed when someone tried to
dereference a pointer to deleted memory location?




它不是这样的无需明确

硬件支持即可轻松确定每次非法内存访问。



It''s not so easy to determine every illegal memory access without explicit
hardware support.


>如果有人试图
取消引用一个指向已删除内存位置的指针,程序崩溃会不会更好?
dereference a pointer to deleted memory location?




更好的是意见问题。调试

肯定会更容易,但如果需要权衡该程序需要10倍的时间才能运行。


虽然你无法让编译器强制执行它,你可以通过在删除指针后始终将指针变为NULL来接近你想要的b / b



删除x;

x = 0;


samuel



Better is a matter of opinion. It would definitely be easier to debug
but what if the trade off was that the program takes 10 times as long
to run.

While you can''t get the compiler to enforce it, you can get close to what
you want by adaptint the style of always NULLing a pointer after deleting it:

delete x;
x = 0;

samuel


这篇关于删除相同的内存位置一段时间后取消引用内存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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