测试以查看对象是否存在新的/删除用法 [英] test to see if object exists with new/delete usage

查看:54
本文介绍了测试以查看对象是否存在新的/删除用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个指向项目的指针:


CardSession * cardSession;


然后,稍后,我使用new来创建一个实例该项目并将其分配给该指针




cardSession = new CardSession();


In另一个函数,我想测试一个对象是否分配给那个

指针,并删除该对象,如果是这样的话:


if(cardSession)

删除cardSession;


我认为这样可行,但后来我发现,如果cardSession

*曾被*分配过一个带有new的CardSession对象,然后if

语句执行delete命令,即使该对象已经被删除了

。因此,我有时会使用此代码将一个项目双重删除 -

一个未定义的行为,我认为这会导致我的程序崩溃。注意

如果我还没有在程序中使用new来将对象分配给

cardSession,则if语句不会执行。


有人可以向我解释一下吗?我是否正确理解指针用法

,还是无法正确使用new / delete?我怎样才能以高效的方式做到这一点,而无需管理每次创建或删除对象时修改的全局指针


I create a pointer to an item:

CardSession *cardSession;

Then, later, I use new to create an instance of the item and assign it
to that pointer:

cardSession = new CardSession();

In another function, I want to test if an object is assigned to that
pointer, and delete the object if that is so:

if( cardSession )
delete cardSession;

I thought this would work, but then I discovered that, if cardSession
had *ever* been assigned a CardSession object with new, then the if
statement executes the delete command, even if the object had since been
deleted. Thus, I sometimes get an item doubly deleted with this code -
an undefined behavior that I think is causing my program to crash. Note
that if I have not yet used new in the program to assign an object to
cardSession, the if statement does not execute.

Could somebody explain this to me? Am I not understanding pointer usage
correctly, or is it innapropriate usage of new/delete? How can I do
this in an efficient fashion, without managing a global pointer to be
modified everytime I create or delete an object?

推荐答案

Squid Seven写道:
Squid Seven wrote:
我创建一个指向项目的指针:

CardSession * cardSession;
<然后,稍后,我使用new来创建项目的实例并将其分配给该指针:

cardSession = new CardSession();
在另一个函数中,我想测试一个对象是否被分配给那个
指针,并删除该对象,如果是这样的话:

if(cardSession)
delete cardSession;

我认为这样可行,但后来我发现,如果cardSession曾经*被赋予了一个带有new的CardSession对象,那么if
语句会执行delete命令,即使该对象已被删除。因此,我有时会使用此代码双重删除项目 -
我认为导致程序崩溃的未定义行为。注意
如果我还没有在程序中使用new来将对象分配给cardSession,则if语句不会执行。

有人可以向我解释这个吗?我是不是正确理解指针使用
,还是无法正确使用new / delete?如何在没有管理每次创建或删除对象时修改的全局指针的情况下以高效的方式执行此操作?
I create a pointer to an item:

CardSession *cardSession;

Then, later, I use new to create an instance of the item and assign it
to that pointer:

cardSession = new CardSession();

In another function, I want to test if an object is assigned to that
pointer, and delete the object if that is so:

if( cardSession )
delete cardSession;

I thought this would work, but then I discovered that, if cardSession
had *ever* been assigned a CardSession object with new, then the if
statement executes the delete command, even if the object had since been
deleted. Thus, I sometimes get an item doubly deleted with this code -
an undefined behavior that I think is causing my program to crash. Note
that if I have not yet used new in the program to assign an object to
cardSession, the if statement does not execute.

Could somebody explain this to me? Am I not understanding pointer usage
correctly, or is it innapropriate usage of new/delete? How can I do
this in an efficient fashion, without managing a global pointer to be
modified everytime I create or delete an object?




意思是说没有管理全球*旗帜*是...... - 抱歉



meant to say "without managing a global *flag* to be..." - sorry


>我创建了一个指向项目的指针:
> I create a pointer to an item:

CardSession * cardSession;


注意这里的指针可能没有NULL值。

[...]
在另一个函数中,我想测试一个对象被分配给那个
指针,并删除对象,如果是这样的话:
if(cardSession)
delete cardSession;


这不测试指针是否指向一个物体。

我以为这会起作用,但后来我发现,如果是cardSession
曾经*曾经*被赋予一个带有new的CardSession对象,然后if
语句执行delete命令,即使该对象已被删除。因此,我有时会使用此代码双重删除项目 -
我认为导致程序崩溃的未定义行为。注意
如果我还没有在程序中使用new来将对象分配给
cardSession,则if语句不会执行。

CardSession *cardSession;
Note that the pointer may not have NULL value here.
[...] In another function, I want to test if an object is assigned to that
pointer, and delete the object if that is so: if( cardSession )
delete cardSession;
This does not test if the pointer points to an object.
I thought this would work, but then I discovered that, if cardSession
had *ever* been assigned a CardSession object with new, then the if
statement executes the delete command, even if the object had since been
deleted. Thus, I sometimes get an item doubly deleted with this code -
an undefined behavior that I think is causing my program to crash. Note
that if I have not yet used new in the program to assign an object to
cardSession, the if statement does not execute.




显然取决于cardSessions的价值。您可以最初或在删除指向的对象后将

指针显式设置为NULL。当cardSession == NULL时,调用delete cardSession是安全的。


问候,

Matthias



Depends on cardSessions''s value obviously. You may explicitly set the
pointer to NULL initially or after deleting the pointed to object. It''s
safe to call delete cardSession when cardSession==NULL.

Regards,
Matthias


Squid Seven写道:
Squid Seven wrote:
我创建一个指向项目的指针:

CardSession * cardSession;


更改为


CardSession * cardSession = null;


这是好的练习初始化变量。

if(cardSession)
删除cardSession;

我认为这样可行,但后来我发现,如果cardSession
有*曾经*被分配了一个带有new的CardSession对象,然后if
语句执行delete命令,即使该对象已被删除。
I create a pointer to an item:

CardSession *cardSession;
Change this to

CardSession *cardSession = null;

It''s good practice to initialize the variable.
if( cardSession )
delete cardSession;

I thought this would work, but then I discovered that, if cardSession
had *ever* been assigned a CardSession object with new, then the if
statement executes the delete command, even if the object had since been
deleted.



是的。要么你不明白如果(cardSession)做了什么,或者你不知道
了解删除的作用。对于指针类型,例如你在这里有
的变量,条件


if(cardSession)


是检查cardSession是否为空指针 - 它确实

不检查指向对象是否有效(并且没有办法在

一般来说,在标准C ++中执行这样的检查。


随后的删除语句会破坏

cardSession所指向的内容。如果一个有效的物体不存在,你可能会崩溃。


你需要做的是什么,以及什么也是好的做法,就是添加这个
删除后



cardSession = null;


还要注意,检查cardSession是否为null不是

必要。删除空指针是绝对安全的(但不是

无效指针),所以只需


删除cardSession;

cardSession = null;


代替原来的if我会做的。


-cjm



Yes. Either you don''t understand what if(cardSession) does, or you don''t
understand what delete does. For a pointer type such as the variable you
have here, the conditional

if(cardSession)

is a check to see whether cardSession is a null pointer or not--it does
NOT check if the pointed-to object is valid (and there is no way in
general to perform such a check in standard C++).

The subsequent delete statement destroys whatever is pointed to by
cardSession. If a valid object isn''t there, you are likely to crash.

What you need to do here, and what is also good practice, is to add this
after you delete:

cardSession = null;

Also be aware that your check for whether cardSession is null is not
necessary. It is perfectly safe to delete a null pointer (but not an
invalid one), so just

delete cardSession;
cardSession=null;

in place of your original "if" will do the job.

-cjm


这篇关于测试以查看对象是否存在新的/删除用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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