question about "delete"

I have a pointer:


MyWindowClass *p = NULL;
p = new MyClass(...);
....
delete p;


After delete p, does p equal NULL(it is in C++ standard?)? How to
decide if p has been deleted?


The reason I asked this question is that in my project, there are many
code/files use the pointer which I need to determine is it is deleted?
Can I use:


if(p != NULL)
    delete p;


I guess somewhere p has been deleted, but p still not NULL(possible?),
the above code might cause problem.

0
yqin_99 (193)
2/3/2006 5:34:09 PM
vc.mfc 33608 articles. 0 followers. Follow

31 Replies
1164 Views

Similar Articles

[PageSpeed] 12

Hi Kathy,

I typically just set p to NULL when I delete it:

delete p;
p = NULL;

Then you can check it in other places and know that it has no memory.  Be 
sure to set p to NULL  in your constructor to to make sure it is initially 
NULL.

Tom

"kathy" <yqin_99@yahoo.com> wrote in message 
news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
>I have a pointer:
>
>
> MyWindowClass *p = NULL;
> p = new MyClass(...);
> ...
> delete p;
>
>
> After delete p, does p equal NULL(it is in C++ standard?)? How to
> decide if p has been deleted?
>
>
> The reason I asked this question is that in my project, there are many
> code/files use the pointer which I need to determine is it is deleted?
> Can I use:
>
>
> if(p != NULL)
>    delete p;
>
>
> I guess somewhere p has been deleted, but p still not NULL(possible?),
> the above code might cause problem.
> 


0
tserface (3860)
2/3/2006 5:55:21 PM
I have never seen or written a code which has tried to validate a delete 
operation
and also I don't see a way it can ever assign the pointer variable passed in 
to
NULL. The CRT delete takes a pass by value atleast and I have never
bothered to get into this level of detail.

It is like validating a statement below:-

int a = 4;

Do you write code under the above declaration like this?:-
if(a == 4)
{
//
}

And if so do you again write code to validate the == operation above?

-- 
Vipin Aravind
"kathy" <yqin_99@yahoo.com> wrote in message 
news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
>I have a pointer:
>
>
> MyWindowClass *p = NULL;
> p = new MyClass(...);
> ...
> delete p;
>
>
> After delete p, does p equal NULL(it is in C++ standard?)? How to
> decide if p has been deleted?
>
>
> The reason I asked this question is that in my project, there are many
> code/files use the pointer which I need to determine is it is deleted?
> Can I use:
>
>
> if(p != NULL)
>    delete p;
>
>
> I guess somewhere p has been deleted, but p still not NULL(possible?),
> the above code might cause problem.
> 


0
vipin (697)
2/3/2006 5:56:43 PM
delete does not change the value of p. After calling delete, p becomes an 
invalid pointer--don't use it.

If you want to use NULL to indicate when p is invalid, then set it to NULL 
after calling delete.

-- 
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resume.htm

"kathy" <yqin_99@yahoo.com> wrote in message 
news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
>I have a pointer:
>
>
> MyWindowClass *p = NULL;
> p = new MyClass(...);
> ...
> delete p;
>
>
> After delete p, does p equal NULL(it is in C++ standard?)? How to
> decide if p has been deleted?
>
>
> The reason I asked this question is that in my project, there are many
> code/files use the pointer which I need to determine is it is deleted?
> Can I use:
>
>
> if(p != NULL)
>    delete p;
>
>
> I guess somewhere p has been deleted, but p still not NULL(possible?),
> the above code might cause problem.
> 


0
jwood (1291)
2/3/2006 6:17:28 PM
"Vipin [MVP]" <Vipin@nospam.com> wrote in message
news:OtxdzsOKGHA.2064@TK2MSFTNGP09.phx.gbl...
> I have never seen or written a code which has tried to validate a delete
> operation
> and also I don't see a way it can ever assign the pointer variable passed
in
> to
> NULL. The CRT delete takes a pass by value atleast and I have never
> bothered to get into this level of detail.

I use the saem method she describes. On instantiation I set appropriate
member variables to NULL and when I delete them I reset to NULL so that
other routines can easily determine if they are valid. I know that may not
be the most complete test of validity but within the app I am developing it
seems to work ok.. I have also wondered if there is a more apporpriate way
to handle this with minimal code.


0
noemail412 (73)
2/3/2006 6:21:42 PM
Even I explicitly set it to NULL, but what she is saying is
that delete internally sets to NULL, which is plainly not TRUE.

-- 
Vipin Aravind

"Nappy" <noemail@all.com> wrote in message 
news:WaNEf.21912$Jd.18113@newssvr25.news.prodigy.net...
>
> "Vipin [MVP]" <Vipin@nospam.com> wrote in message
> news:OtxdzsOKGHA.2064@TK2MSFTNGP09.phx.gbl...
>> I have never seen or written a code which has tried to validate a delete
>> operation
>> and also I don't see a way it can ever assign the pointer variable passed
> in
>> to
>> NULL. The CRT delete takes a pass by value atleast and I have never
>> bothered to get into this level of detail.
>
> I use the saem method she describes. On instantiation I set appropriate
> member variables to NULL and when I delete them I reset to NULL so that
> other routines can easily determine if they are valid. I know that may not
> be the most complete test of validity but within the app I am developing 
> it
> seems to work ok.. I have also wondered if there is a more apporpriate way
> to handle this with minimal code.
>
> 


0
vipin (697)
2/3/2006 6:26:28 PM
On 3 Feb 2006 09:34:09 -0800, "kathy" <yqin_99@yahoo.com> wrote:

>I have a pointer:
>
>
>MyWindowClass *p = NULL;
>p = new MyClass(...);
>...
>delete p;
>
>
>After delete p, does p equal NULL(it is in C++ standard?)?

No, it has an indeterminate value, and it's undefined to inspect it.

>How to
>decide if p has been deleted?

You keep track of this yourself. One way is to set it to NULL immediately
after the delete; this will allow you to compare it to NULL later on.

>The reason I asked this question is that in my project, there are many
>code/files use the pointer which I need to determine is it is deleted?
>Can I use:
>
>
>if(p != NULL)
>    delete p;
>
>
>I guess somewhere p has been deleted, but p still not NULL(possible?),
>the above code might cause problem.

Deleting a NULL pointer is harmless, so there is no point in comparing to
NULL above. Now consider non-NULL pointers. If they are valid, deleting
will have the desired effect. If they are invalid, you have a program bug.
Comparing to NULL doesn't help you distinguish between the two, so it's
just completely pointless to compare to NULL solely to determine whether
the pointer should be deleted.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/3/2006 6:28:05 PM
Hi Vipin.  I think she was asking if it would or not.  It's a fair question 
especially since it does get a default value in debug mode (something I find 
handy).

I think the method we're all espousing is the best way to do it really in 
just about any language that uses pointers.

Tom

"Vipin [MVP]" <Vipin@nospam.com> wrote in message 
news:OEFyb9OKGHA.3120@TK2MSFTNGP10.phx.gbl...
> Even I explicitly set it to NULL, but what she is saying is
> that delete internally sets to NULL, which is plainly not TRUE.
>
> -- 
> Vipin Aravind
>
> "Nappy" <noemail@all.com> wrote in message 
> news:WaNEf.21912$Jd.18113@newssvr25.news.prodigy.net...
>>


0
tserface (3860)
2/3/2006 6:59:46 PM
I think others have coverd the ground. I always assign the pointer to
NULL after deleting. As Doug pointed out, deleting a Null pointer is
safe.

On the same lines, I use the some of the following liberally. What you
really want to know is if the memory pointed to by the pointer is good.
This will help:

- ASSERT_VALID
- ASSERT_POINTER
- ASSERT_NULL_OR_POINTER
- AfxIsValidAddress
- AfxIsValidString
- ASSERT_KINDOF

Some of these wrap IsBad*Ptr API.

---------
Ajay Kalra
ajaykalra@yahoo.com

0
ajaykalra (6842)
2/3/2006 7:08:52 PM
int *ptr = new ptr[100 * sizeof(int)]; //allocates 100  integer objects.

delete [] ptr; // This will NOT set "ptr" to NULL nor to any OTHER value.
It can never modify the value of ptr to anything else, it will remain the 
same as what ptr was.

yes, what you mean by getting default value in the debug mode is the memory 
to which
it was pointing to. That would get filled with 0xDD in debug mode.

-- 
Vipin Aravind

"Tom Serface" <tserface@msn.com> wrote in message 
news:%23sW3BQPKGHA.2248@TK2MSFTNGP15.phx.gbl...
> Hi Vipin.  I think she was asking if it would or not.  It's a fair 
> question especially since it does get a default value in debug mode 
> (something I find handy).
>
> I think the method we're all espousing is the best way to do it really in 
> just about any language that uses pointers.
>
> Tom
>
> "Vipin [MVP]" <Vipin@nospam.com> wrote in message 
> news:OEFyb9OKGHA.3120@TK2MSFTNGP10.phx.gbl...
>> Even I explicitly set it to NULL, but what she is saying is
>> that delete internally sets to NULL, which is plainly not TRUE.
>>
>> -- 
>> Vipin Aravind
>>
>> "Nappy" <noemail@all.com> wrote in message 
>> news:WaNEf.21912$Jd.18113@newssvr25.news.prodigy.net...
>>>
>
> 


0
vipin (697)
2/3/2006 7:15:27 PM
sorry my bluder when typing the first line should be read as
int *ptr = new int[100];

-- 
Vipin Aravind

"Vipin [MVP]" <Vipin@nospam.com> wrote in message 
news:%23ZKizYPKGHA.3944@tk2msftngp13.phx.gbl...
> int *ptr = new ptr[100 * sizeof(int)]; //allocates 100  integer objects.
>
> delete [] ptr; // This will NOT set "ptr" to NULL nor to any OTHER value.
> It can never modify the value of ptr to anything else, it will remain the 
> same as what ptr was.
>
> yes, what you mean by getting default value in the debug mode is the 
> memory to which
> it was pointing to. That would get filled with 0xDD in debug mode.
>
> -- 
> Vipin Aravind
>
> "Tom Serface" <tserface@msn.com> wrote in message 
> news:%23sW3BQPKGHA.2248@TK2MSFTNGP15.phx.gbl...
>> Hi Vipin.  I think she was asking if it would or not.  It's a fair 
>> question especially since it does get a default value in debug mode 
>> (something I find handy).
>>
>> I think the method we're all espousing is the best way to do it really in 
>> just about any language that uses pointers.
>>
>> Tom
>>
>> "Vipin [MVP]" <Vipin@nospam.com> wrote in message 
>> news:OEFyb9OKGHA.3120@TK2MSFTNGP10.phx.gbl...
>>> Even I explicitly set it to NULL, but what she is saying is
>>> that delete internally sets to NULL, which is plainly not TRUE.
>>>
>>> -- 
>>> Vipin Aravind
>>>
>>> "Nappy" <noemail@all.com> wrote in message 
>>> news:WaNEf.21912$Jd.18113@newssvr25.news.prodigy.net...
>>>>
>>
>>
>
> 


0
vipin (697)
2/3/2006 7:17:40 PM
Something funny.  I knew a guy once who didn't like NULL for some reason or
another, so he used 0xdeadbeef as null!

AliR.

"kathy" <yqin_99@yahoo.com> wrote in message
news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
> I have a pointer:
>
>
> MyWindowClass *p = NULL;
> p = new MyClass(...);
> ...
> delete p;
>
>
> After delete p, does p equal NULL(it is in C++ standard?)? How to
> decide if p has been deleted?
>
>
> The reason I asked this question is that in my project, there are many
> code/files use the pointer which I need to determine is it is deleted?
> Can I use:
>
>
> if(p != NULL)
>     delete p;
>
>
> I guess somewhere p has been deleted, but p still not NULL(possible?),
> the above code might cause problem.
>


0
AliR3470 (3235)
2/3/2006 7:22:35 PM
No problem.  We totally agree...

Tom

"Vipin [MVP]" <Vipin@nospam.com> wrote in message 
news:%23ZKizYPKGHA.3944@tk2msftngp13.phx.gbl...
> int *ptr = new ptr[100 * sizeof(int)]; //allocates 100  integer objects.
>
> delete [] ptr; // This will NOT set "ptr" to NULL nor to any OTHER value.
> It can never modify the value of ptr to anything else, it will remain the 
> same as what ptr was.
>
> yes, what you mean by getting default value in the debug mode is the 
> memory to which
> it was pointing to. That would get filled with 0xDD in debug mode.


0
tserface (3860)
2/3/2006 7:39:17 PM
LOL...

A herd of cattle with legs on one side == "Lean beef"
A herd of cattle with no legs == "Ground beef"
A herd of dead cattle == "Deleted beef"

Tom

"AliR" <AliR@online.nospam> wrote in message 
news:%3OEf.29628$H71.9748@newssvr13.news.prodigy.com...
> Something funny.  I knew a guy once who didn't like NULL for some reason 
> or
> another, so he used 0xdeadbeef as null!
>
> AliR.


0
tserface (3860)
2/3/2006 7:40:23 PM
On Fri, 03 Feb 2006 19:22:35 GMT, "AliR" <AliR@online.nospam> wrote:

>Something funny.  I knew a guy once who didn't like NULL for some reason or
>another, so he used 0xdeadbeef as null!

I think you mean he used it instead of NULL. Values such as 0xDEADBEEF are
very useful and have a long history. They're used when you need to signify
something besides NULL without mistaking it for a valid pointer value, and
they stand out nicely in memory dumps. Of course, the available values
depend on the platform. For an ordinary Windows process, 1, 2, 3 up to (I
think) 64K can all be used for this purpose, since this area of memory is
never mapped into a process. (Actually, Richter says in "Programming Apps
for Windows" that 0-64K is the "null pointer partition" for NT-based
Windows, while 0-4K is the partition for Win98.)

Note that 0xDEADBEEF can help you catch unintended double-deletion errors.
Although deleting a NULL pointer is harmless, the attempt may indicate a
bug in your program logic. The heap manager obviously can't distinguish
between intentional and unintentional NULL pointer deletions, but it can
raise an error for attempts to delete invalid pointer values.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/3/2006 7:40:24 PM
"Vipin [MVP]" <Vipin@nospam.com> wrote in message 
news:%23aWnCaPKGHA.964@tk2msftngp13.phx.gbl...
> sorry my bluder when typing the first line should be read as
> int *ptr = new int[100];
>

On the bright side, you used the proper form of operator delete!  ;)

PS: If the posters in this thread like the behavior of the debug version of 
the operator, they can always overload the standard library version and 
implement behavior that explicitly sets the pointer to null.

-Pete 


0
2/3/2006 7:47:45 PM
I have been swapping between writting newsgroup
posts and the writting code in a program here. So I think something got
jumbled up in mind when I was doing two things at a time. :)
-- 
Vipin Aravind

"Pete Delgado" <Peter.Delgado@noads.net> wrote in message 
news:%23MSaZqPKGHA.2992@tk2msftngp13.phx.gbl...
>
> "Vipin [MVP]" <Vipin@nospam.com> wrote in message 
> news:%23aWnCaPKGHA.964@tk2msftngp13.phx.gbl...
>> sorry my bluder when typing the first line should be read as
>> int *ptr = new int[100];
>>
>
> On the bright side, you used the proper form of operator delete!  ;)
>
> PS: If the posters in this thread like the behavior of the debug version 
> of the operator, they can always overload the standard library version and 
> implement behavior that explicitly sets the pointer to null.
>
> -Pete
> 


0
vipin (697)
2/3/2006 7:53:25 PM
On Fri, 3 Feb 2006 14:47:45 -0500, "Pete Delgado" <Peter.Delgado@noads.net>
wrote:

>PS: If the posters in this thread like the behavior of the debug version of 
>the operator, they can always overload the standard library version and 
>implement behavior that explicitly sets the pointer to null.

Q. What do you think the "debug version" does differently?

Q. How do you propose to "overload" the standard library version?

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/3/2006 9:13:06 PM
I just checked Stroustrup 3rd edition, and there is no suggestion that delete p will set
the variable p to NULL.  

If you care, set p to NULL yourself.

There is no need to conditionalize delete; if p is NULL, delete does nothing.  This *is*
part of the C++ definition.
				joe

On 3 Feb 2006 09:34:09 -0800, "kathy" <yqin_99@yahoo.com> wrote:

>I have a pointer:
>
>
>MyWindowClass *p = NULL;
>p = new MyClass(...);
>...
>delete p;
>
>
>After delete p, does p equal NULL(it is in C++ standard?)? How to
>decide if p has been deleted?
>
>
>The reason I asked this question is that in my project, there are many
>code/files use the pointer which I need to determine is it is deleted?
>Can I use:
>
>
>if(p != NULL)
>    delete p;
>
>
>I guess somewhere p has been deleted, but p still not NULL(possible?),
>the above code might cause problem.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
2/3/2006 9:28:38 PM
"deadbeef" is what the IBM RISC 6000 OS used for uninitialized storage (e.g., the
0xCDCDCDCD or 0xDFDFDFDF equivalent in the debug libraries).  Of course, setting pointers
that are expected to be NULL to any non-NULL value is likely to be fatal.
					joe

On Fri, 03 Feb 2006 19:22:35 GMT, "AliR" <AliR@online.nospam> wrote:

>Something funny.  I knew a guy once who didn't like NULL for some reason or
>another, so he used 0xdeadbeef as null!
>
>AliR.
>
>"kathy" <yqin_99@yahoo.com> wrote in message
>news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
>> I have a pointer:
>>
>>
>> MyWindowClass *p = NULL;
>> p = new MyClass(...);
>> ...
>> delete p;
>>
>>
>> After delete p, does p equal NULL(it is in C++ standard?)? How to
>> decide if p has been deleted?
>>
>>
>> The reason I asked this question is that in my project, there are many
>> code/files use the pointer which I need to determine is it is deleted?
>> Can I use:
>>
>>
>> if(p != NULL)
>>     delete p;
>>
>>
>> I guess somewhere p has been deleted, but p still not NULL(possible?),
>> the above code might cause problem.
>>
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
2/3/2006 9:32:17 PM
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message 
news:jah7u15bj5df16p1tqp1vpb5ahec84253q@4ax.com...
> On Fri, 3 Feb 2006 14:47:45 -0500, "Pete Delgado" 
> <Peter.Delgado@noads.net>
> wrote:
>
>>PS: If the posters in this thread like the behavior of the debug version 
>>of
>>the operator, they can always overload the standard library version and
>>implement behavior that explicitly sets the pointer to null.
>
> Q. What do you think the "debug version" does differently?

The debug version of operator delete performs a memset on the allocated 
block to ensure that the block is not mistaken for valid data after it has 
been freed.  It also performs checks on the bounds of the block to ensure 
that there haven't been any memory overwrites. In addition, it performs 
several other checks on the memory block.

>
> Q. How do you propose to "overload" the standard library version?

In the manner described by the documentation.  Define a class-scoped 
operator delete while tracking allocations and deallocations.  Using the 
tracking information, one could explicitly set the pointer to NULL.

See:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/specl_29.asp
and
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_new_Operatordelete.asp
for more information.

As to whether the additional overhead and restrictions are worth the cost, I 
leave that for others to decide!  ;)

PS: I noticed that it appears that I am stating that the debug version of 
the standard library operator sets the pointer equal to null, it was simply 
a poor choice of wording for the sentance.

-Pete 


0
2/3/2006 10:05:48 PM
Great idea...

Tom

"Pete Delgado" <Peter.Delgado@noads.net> wrote in message 
news:%23MSaZqPKGHA.2992@tk2msftngp13.phx.gbl...
>
> "Vipin [MVP]" <Vipin@nospam.com> wrote in message 
> news:%23aWnCaPKGHA.964@tk2msftngp13.phx.gbl...
>> sorry my bluder when typing the first line should be read as
>> int *ptr = new int[100];
>>
>
> On the bright side, you used the proper form of operator delete!  ;)
>
> PS: If the posters in this thread like the behavior of the debug version 
> of the operator, they can always overload the standard library version and 
> implement behavior that explicitly sets the pointer to null.
>
> -Pete
> 


0
tserface (3860)
2/3/2006 10:18:09 PM
That code looks scary:

1. I hope the MyWindowClass and MyClass classes have virtual destructors.

2. I would definitely consider rewriting it and use something like 
std::auto_ptr in separate scopes.


"kathy" <yqin_99@yahoo.com> wrote in message 
news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
>I have a pointer:
>
>
> MyWindowClass *p = NULL;
> p = new MyClass(...);
> ...
> delete p;
>
>
> After delete p, does p equal NULL(it is in C++ standard?)? How to
> decide if p has been deleted?
>
>
> The reason I asked this question is that in my project, there are many
> code/files use the pointer which I need to determine is it is deleted?
> Can I use:
>
>
> if(p != NULL)
>    delete p;
>
>
> I guess somewhere p has been deleted, but p still not NULL(possible?),
> the above code might cause problem.
> 


0
2/3/2006 10:27:09 PM
On Fri, 3 Feb 2006 17:05:48 -0500, "Pete Delgado" <Peter.Delgado@noads.net>
wrote:

>"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message 
>news:jah7u15bj5df16p1tqp1vpb5ahec84253q@4ax.com...
>> On Fri, 3 Feb 2006 14:47:45 -0500, "Pete Delgado" 
>> <Peter.Delgado@noads.net>
>> wrote:
>>
>>>PS: If the posters in this thread like the behavior of the debug version 
>>>of
>>>the operator, they can always overload the standard library version and
>>>implement behavior that explicitly sets the pointer to null.
>>
>> Q. What do you think the "debug version" does differently?
>
>The debug version of operator delete performs a memset on the allocated 
>block to ensure that the block is not mistaken for valid data after it has 
>been freed.  It also performs checks on the bounds of the block to ensure 
>that there haven't been any memory overwrites. In addition, it performs 
>several other checks on the memory block.

Not the answer I expected, but OK. :)

>> Q. How do you propose to "overload" the standard library version?
>
>In the manner described by the documentation.  Define a class-scoped 
>operator delete while tracking allocations and deallocations.  Using the 
>tracking information, one could explicitly set the pointer to NULL.
>
>See:
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/specl_29.asp
>and
>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_new_Operatordelete.asp
>for more information.
>
>As to whether the additional overhead and restrictions are worth the cost, I 
>leave that for others to decide!  ;)

If you mean storing pointers in a map for later lookup, the usual objection
is that there may be multiple copies of the pointer in existence, and you
won't get them all. Also, it's a sort of "crutch technique" that's really
at odds with the language. You'll never be able to apply it universally,
and so you'll end up worrying about which classes it applies to and which
it does not. I think you'd be better off with something like:

template<typename T>
void
delete_and_nullify(T*& p)
{
   delete p;
   p = 0;
}

>PS: I noticed that it appears that I am stating that the debug version of 
>the standard library operator sets the pointer equal to null

That's what I thought, too. :)

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/4/2006 3:35:59 AM
I think IsBadPtr API only means IsReallyBadPtr. It may not detect freed, but 
still accessible memory, or other such cases.

"Ajay Kalra" <ajaykalra@yahoo.com> wrote in message 
news:1138993732.736163.141120@g47g2000cwa.googlegroups.com...
>I think others have coverd the ground. I always assign the pointer to
> NULL after deleting. As Doug pointed out, deleting a Null pointer is
> safe.
>
> On the same lines, I use the some of the following liberally. What you
> really want to know is if the memory pointed to by the pointer is good.
> This will help:
>
> - ASSERT_VALID
> - ASSERT_POINTER
> - ASSERT_NULL_OR_POINTER
> - AfxIsValidAddress
> - AfxIsValidString
> - ASSERT_KINDOF
>
> Some of these wrap IsBad*Ptr API.
>
> ---------
> Ajay Kalra
> ajaykalra@yahoo.com
> 


0
alegr (1131)
2/4/2006 5:07:24 AM
Sorry, 'operator delete' receives void *, not PVOID&.

"Pete Delgado" <Peter.Delgado@noads.net> wrote in message 
news:%23CrSi3QKGHA.2696@TK2MSFTNGP14.phx.gbl...
>
>
> In the manner described by the documentation.  Define a class-scoped 
> operator delete while tracking allocations and deallocations.  Using the 
> tracking information, one could explicitly set the pointer to NULL.
>


0
alegr (1131)
2/4/2006 5:11:25 AM
Source code for all the memory allocation routines is there in the crt 
directory
and in afxmem.cpp for MFC. Just step in on a new and delete operation and 
get the feel.

Who said microsoft  hides everything? :) There is something to rejoice.
We just need the source code of the Windows APIs too. Are they up for it?

-- 
Vipin Aravind

"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message 
news:jah7u15bj5df16p1tqp1vpb5ahec84253q@4ax.com...
> On Fri, 3 Feb 2006 14:47:45 -0500, "Pete Delgado" 
> <Peter.Delgado@noads.net>
> wrote:
>
>>PS: If the posters in this thread like the behavior of the debug version 
>>of
>>the operator, they can always overload the standard library version and
>>implement behavior that explicitly sets the pointer to null.
>
> Q. What do you think the "debug version" does differently?
>
> Q. How do you propose to "overload" the standard library version?
>
> -- 
> Doug Harrison
> Visual C++ MVP 


0
vipin (697)
2/4/2006 7:51:28 AM
On Fri, 3 Feb 2006 21:11:25 -0800, "Alexander Grigoriev"
<alegr@earthlink.net> wrote:

>"Pete Delgado" <Peter.Delgado@noads.net> wrote in message 
>news:%23CrSi3QKGHA.2696@TK2MSFTNGP14.phx.gbl...
>>
>>
>> In the manner described by the documentation.  Define a class-scoped 
>> operator delete while tracking allocations and deallocations.  Using the 
>> tracking information, one could explicitly set the pointer to NULL.
>
>Sorry, 'operator delete' receives void *, not PVOID&.

That's what I thought he was getting at when I wrote my "questions"
message, but his answer suggests a map, not an illegal operator delete. The
map approach doesn't deal with pointer aliasing and lifetime issues, so I
don't see how it can work.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/4/2006 5:41:00 PM
On Sat, 4 Feb 2006 13:21:28 +0530, "Vipin [MVP]" <Vipin@nospam.com> wrote:

>Source code for all the memory allocation routines is there in the crt 
>directory
>and in afxmem.cpp for MFC. Just step in on a new and delete operation and 
>get the feel.

Whoosh. :) Note the two questions I asked were in the form, "How do you..."
with the operative word being "you". There was a reason for that, which
should be clear if you read the other messages.

>Who said microsoft  hides everything? :) There is something to rejoice.
>We just need the source code of the Windows APIs too. Are they up for it?

As you are an MVP, see:

http://mvp.support.microsoft.com/mvptechres

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/4/2006 5:41:00 PM
>>Who said microsoft  hides everything? :) There is something to rejoice.
>>We just need the source code of the Windows APIs too. Are they up for it?
>
> As you are an MVP, see:
>
> http://mvp.support.microsoft.com/mvptechres


Unfortunately some of them are not applicable in my country, I knew of this 
previously.
MS doesn't show it to me. May be I should think of migrating to US or Europe
to which I have been having bigger inertia till now.

-- 
Vipin Aravind 


0
vipin (697)
2/4/2006 6:00:33 PM
"Alexander Grigoriev" <alegr@earthlink.net> wrote in message 
news:OCc4qkUKGHA.984@tk2msftngp13.phx.gbl...
> Sorry, 'operator delete' receives void *, not PVOID&.

Please reread my post.  That is *not* what I stated.

-Pete 


0
Pete4450 (53)
2/5/2006 8:00:04 AM
"kathy" <yqin_99@yahoo.com> wrote in message
news:1138988049.456345.161680@g43g2000cwa.googlegroups.com...
>
> MyWindowClass *p = NULL;
> p = new MyClass(...);
> ...
> delete p;
>
> After delete p, does p equal NULL(it is in C++ standard?)?

No.

> How to
> decide if p has been deleted?

You can't, without doing some extra work.

> The reason I asked this question is that in my project, there are many
> code/files use the pointer which I need to determine is it is deleted?
> Can I use:
>
> if(p != NULL)
>     delete p;

No. But you can use:

if ( p )
{
    delete p;
    p = 0;
}

(if coding in C++, which you appear to be; the NULL business, with all the
difficulties of declaring different versions of it for data and code in
mixed 16/32 bit pointer memory models, is a hangover from C).

--
Tim Ward
Brett Ward Limited - www.brettward.co.uk


0
tw2 (223)
2/6/2006 9:53:43 AM
Reply:

Similar Artilces:

Haunted Deleted Mail
I use OL2003. When I delete mail and empty the deleted item folder I would expect the items to be gone. I close OL and next time I open OL there are the deleted items ,back in the originating folders !! Weird ! I'm beginning to think my OL is haunted. Jeff "Jeff" <jpy1@nospam.com> wrote in message news:OT0gzjz4FHA.1140@tk2msftngp13.phx.gbl... >I use OL2003. > When I delete mail and empty the deleted item folder I would expect the > items to be gone. I close OL and next time I open OL there are the deleted > items ,back in the originating folders !! Weird ! ...

Outlook Delete folder problems
I can not permenately delete messages from Outlook 2000. I have run the Inbox Repair tool twice and it has found nothing. Where do I go from here? I also am unable to delete messages from my Inbox as well. Thanks, Kim Kim@traveldog.com ...

How do I delete Publisher 2003 user templates?
I created my own Publisher 2003 template. I was unable to find a way in Publisher to delete this user template. Did I miss something? I know that I can go to C:\Documents & Settings\.... Microsoft\Office\Templates and delete it there. Thanks, Ken You have to delete it in the folder you mentioned. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "kenlanderson" <kenlanderson@discussions.microsoft.com> wrote in message news:46C373CC-D111-4DE6-81F7-C4AD6BE065B1@microsoft.com... >I created my own Publisher 2003 t...

delete an array
I know how to edit an array but I cannot figure out how to take it off. Everytime I try I keep getting a message that says cannot edit a cell in a array. Thanks A You have to select the whole array and then delete it -- Regards, Peo Sjoblom "anton" <anton@discussions.microsoft.com> wrote in message news:016BDACF-DEE2-4756-A29C-200610DEF4DB@microsoft.com... > I know how to edit an array but I cannot figure out how to take it off. > Everytime I try I keep getting a message that says cannot edit a cell in a > array. Thanks A ...

question about "delete"
I have a pointer: MyWindowClass *p = NULL; p = new MyClass(...); .... delete p; After delete p, does p equal NULL(it is in C++ standard?)? How to decide if p has been deleted? The reason I asked this question is that in my project, there are many code/files use the pointer which I need to determine is it is deleted? Can I use: if(p != NULL) delete p; I guess somewhere p has been deleted, but p still not NULL(possible?), the above code might cause problem. Hi Kathy, I typically just set p to NULL when I delete it: delete p; p = NULL; Then you can check it in other places and ...

Deleted user confused with active user w/same last name
There was a user in our org that is now deleted from the system that has the same last name as an active user still in the system. When someone tries to invite the active user to a meeting they are getting an email error back stating that the deleted user cannot be found in Exchange. The deleted user is not being selected in Outlook for the meeting but for some reason Outlook is trying to invite the wrong user to the meeting. I can't figure out why this is happening, does anyone have any suggestions what I can look for to correct this? Thanks. On 23 Jan 2006 18:25:39 -0800, tdunnusa@...

Cannot delete files in my local folders
Can anyone help ...

I need to delete duplicates inside of spreadsheet
I have an email database that has duplicate entries and I need to know how to search them and delete duplicates. Then I need to merge 2 different spreadsheets together. Help!!! Hi, Copy and paste all the email addresses into one list. Select the first cell in the list. From the Data menu, select Filter > Advanced Filter. In the advanced filter window: Check the "Copy to another location" thingy, Tick Unique records only thingy, In the Copy to field, enter the start cell for where you want the list of uniques to go OK Unique items are now listed in a new place, and you can d...

Add the option to delete files in FRX report Launcher
The option to delete the FRD files after the report has been viewed, printed, or sent is availible in the report designer. This option is not availible int he report launcher and leaves the FRD file in the I/O Data folder. This can be a security risk for companies with multiple divisions if the user generating the reports forgets to set a password on the report, as anyone will be able to browse to the I/O data folder and view the report. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, c...

Outlook 2007 keeps trying to send a deleted mail
Hi, I had problems today to send a mail via Outlook 2007 (on WinXP Pro). Each time I did a send/receive all, Outlook would try to send the mail, but to no avail. Finally, I found that there was a problem with the recipient addresses (which I copy-pasted from a text file, and which were apparently in a wrong format, because the Send/Receive error tab showed a "501 Bad address syntax" error). I decided to delete the mail from the outbox and recreated it from scratch with the correct recipient addresses. So far so good, but since then, on each Send/Receive cyle, Outlook is tryin...

My question gets deleted
Why is my question gets deleted ? I posted twice and it got deleted twice.... I'm getting mad... who has the authority to delete posts in this group ? Julien, Your posts are there. Are you sure your newsgroup reader is not set to hide postes that have already been viewed? Marc "Julien Bonnier" <julien@m0851.com> wrote in message news:OdGmzENGJHA.1272@TK2MSFTNGP02.phx.gbl... > Why is my question gets deleted ? > > > I posted twice and it got deleted twice.... I'm getting mad... who has the > authority to delete posts in this group ? > ...

delete quintuplicate rows of data in spreadsheet
After importing data from a daily racing form into an excel spreadsheet I will have the lifetime past performance of up to 100 +- race horses so Column A will contain as many as 15 to 20 races of the same (horse) name even though the data in Column B through ...........................(whatever column) will be unique. I only need to use 4 races from each horse, I would like a way to filter data so that only 4 races of each horse are available, the rest will be deleted. If exporting it into MS Access is a way to do it that is OK too!! "goognodoubt" <jamesnodoubt@hotmail.com> w...

How to delete a site from the organization when the server is unavailabe
I have an exchange 5.5 organization with 3 sites NY, Chicago1 and Chicago2. Site Chicago2 has one server and the other two sites have 2 servers each. What has happened is that the server in Chicago2 site was moved to another organization (company merger/sale) and is not inaccessible. I am not able to remove the server or the site from my organization. I am not able to remove the replication or the connectors (I understand that this is the first step). The reason why I need the site removed is because I have to forward e-mails which resided in that box to the new company. (using custom recipein...

E2K Deleted Messages
Greetings, My supervisor came to me today asking if we could find out if a large amount of messages have been deleted from a mailbox. They are looking for 15-30 days worth of the most recent activity. Is there anything that I can do to provide them with this information. I do not have item retention turned on for deleted items, only for mailboxes. Any help you can provide would be appreciated. I don't see how you'd be able to find out, then... -- Susan Conkey [MVP] "Keith" <Keith@discussions.microsoft.com> wrote in message news:9F1DE479-8DBF-4261-A6F1-4E9E462...

deleted all users from a sub site
We created a sub site to test the different options available in SharePoint as well as to try and see what things the users could do that would cause us problems. For one sub site all of the user groups were removed from the site...which instantly gave me no access to the sub site. when I request access it sends the email to me, but I can't go in and approve it. I can't delete the testing sub site because it says it still has its own sub site...yet I cannot see it or access it. so I have two questions... How can I get back into that sub site to delete it and how do...

Deleted ".pst" file
When opening Outlook 2000 this morning, I received the message that Outlook could not find the "outlook.pst" file? I have searched for it and cannot find it anywhere on my PC! This file contains a large amount of irreplaceable data that I need to locate. No one has been operating my machine and I feel like the file is still on the hard drive. I would really appreciate any advice on this issue. Thanks, Lynn Fazio As the data is 'irreplaceable' surely you have a backup? The file may be a hidden file, search for *.pst "Lynn Fazio" <lynnfazio@triconinc....

Accidentally deleted all recurring events (birthdays).
Is there a way to add all of them at once of do I have to go into each contact and, on the details page, reenter the date, and hit save. Easier to Undelete them from the Deleted items folder. Shleets wrote: > Is there a way to add all of them at once of do I have to go into each > contact and, on the details page, reenter the date, and hit save. ...

Creating a user form that deletes specific parapgraphs
hi, hope you can help. Im trying to create a document for work, and ive been confused how best to do it. I have a letter that contains 3 seperate paragraphs. When opening the letter i want a user form to open, requiring the user to make a selection of which paragraph they need to use (only one of the paragraphs will be used on each letter). So, when the user makes the seleciton and confirms, the other 2 paragraphs are removed. Im really struggling, and am very basic at VBA. any suggestions welcome. It would be simpler to allow the user to pick which paragraph to use then e...

Delete records from second table
Hello I have two Tables. tblMain(MainID) tblSec(FamID, Group, MainID) MainID has same attribute in both tables tblMain has the correct records. How can I delete all records from tblSec not present in tblMain? Could you tell me the kind of query. I'm studying Relational Algebra by myself and this example would help me to learn Thanks in advance Do you want to delete the records from the table or just SHOW the records? SELECT tblSec.* FROM tblSec INNER JOIN tblMain ON tblSec.MainID = tblMain.MainID DELETE FROM TblSec WHERE MainID NOT IN (SELECT MainID FROM tblMain)...

Automatically delete old items off the server?
Is there any way we can delete old items from the server itself? Sure, we can use policies on Outlook, but many of our users are just using OWA and we want their old email to get deleted after 120 days. Any way this can be done? TIA! / Per Use Recipient Policy with Mailbox Manager settings. Recommend using this in "generate report only" mode till you're confident the policy has picked up the right users, is doing/will do what you want it to do, and users are informed about this (so they have time to backup/archive their stuff). -- Bharat Suneja MVP - Exchange www.zen...

Delete incomplete/bad rows?
I've a worksheet that imports data from a text file every time it's opened. How do I automatically search the sheet with the imported data for incomplete rows and delete these rows? Note: an incomplete row has less or more filled cells than 10 (some data is missing or junk is added to the row!) Are you sure that is a valid criteria. What if 5 cells of data were missing, and 5 pieces of junk got added to other blank cells - You still have 10 cells. Anyway, taking that as your criteria, here is a tweak on one of Chip Pearson's for deleting blank rows:- (For deleting blank rows...

Retrieving deleted messages
Is there any way to retrieve a message that was deleted when I emptied the "deleted items folder"? I very much need to get to this email. Help please.... No, sorry. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the SWEN virus, all mail sent to my personal account will be deleted without reading. After searching google.groups.com and finding no answer, Dilliejane asked: | Is there any way to retrieve a message that was deleted | when I emptied the "deleted items folder"? I very much | need to get to this email. ...

large email in exchange unable to delete
Hello, I've been looking for a solution, but nothing yet has worked. I have a user on Outlook 2003 who tried to send a 600MB file two days ago. it didn't go through, but she didn't receive a "could not send" notice, it's not in her sent mail folder, and she's now getting size limit warnings. Her local mailbox size doesn't reflect the size of the attachment, but using Exchange System Manager her total size is 600MB higher. Nothing that we have tried on the client has worked to remove the email, including procedures that have been suggested on this newsgroup. D...

Deleted Items #3
I have used Outlook Express for a long time, and have recently made the move to Outlook 2002. One gripe and something that is very bothersome to me is that when I go to my Deleted Items view, I not only see deleted mail, but everything that I've deleted (appointments, tasks, contacts, etc.) Is there anyway that I can view ONLY deleted mail in my Deleted Items view? No, your deleted "items" folder contains all of your deleted items, not just mail. As Outlook Express only functions for mail and news, your deleted items probably only contained mail items. As an aside, why...

Error deleting record
I have a form and subform combination for entering two related records, an Inventory item Master definition and an Inventory item sub definition. The master information is on the main form and the sub definition is on the subform. I have coded logic to delete the master record that is automatically written by Access when the user tabs from the main form into the subform IN THE EVENT that the user enters no information into the subform and exits from/closes the main form using the exit button I've provided. When I test this code, I get error 2046 "The command or action acCmd...