473,779 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set a pointer to null when deleting?

When you delete a pointer, you should set it to NULL, right?

Joe
Apr 10 '06
51 10482

peter koch wrote:
Noah Roberts skrev:
peter koch wrote:
Noah Roberts skrev: > And there it is. There is no reason NOT to beyond linespace fetishes.

There is one very good reason: to verify your program logic. Accessing
a resource that is no longer present is an obvious programming error,
and setting the pointer to 0 might just prevent that situation from
being identified.
Not true.

Sure? Examples given in this thread would specifically behave that way.
Far to often you write if (p != 0) p->action() instead of assert(p !=
0); p->action();


How are you applying logic here?

1) what example of pointer access would work if ptr is 0 and not
otherwise?
2) what is the relevance of the assert statement you make above?
If you are going to set the pointer to anything, it is a far better
idea to set it to some illegal value the same way as operator delete
sets memory to an often illegal pattern in some debug builds.


This idea is very non-portable for one and doesn't follow standard
practice. Talk about being difficult to understand...

There is absolutely no reason to go looking for some other value to set
a pointer to. All that does is introduce problems in both
understandabili ty, platform dependance, and undefined behavior.

That is a small problem as this is only something to do while
debugging.


Yes, it is good to introduce hard to read, platform dependant,
undefined behavior during debugging and then replace it with different
code on release.

The arguments against setting a pointer to 0 after deletion just keep
getting dumber and dumber...I don't really find that surprising.

Apr 11 '06 #41
Noah Roberts wrote:
The arguments against setting a pointer to 0 after deletion just keep
getting dumber and dumber...I don't really find that surprising.


Smart code...

- uses smart pointers
- uses references
- use objects with fail-safe behaviors
- reduce 'if' statements on types*
- practices RAII
- generally only delete pointers about to go out of scope
- assign NULL to the tiny fraction of remaining pointers

*If I said "NULL is a type" here, I would get an entire thread of yacking
"NULL is a macro typically defined as 0 which is an integer,
blah-blah-blah". The book Design Patterns calls this topic "program to the
interface". Checking a pointer for NULL is a sign the design could be
generally better. It's using the interface to a pointer instead of an
interface to an object.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 11 '06 #42
Joe Van Dyk wrote:
Victor Bazarov wrote:
class B {
public:
void foo(A* aptr);

private:
A* aptr_;
};

void B::foo(A* aptr)
{
aptr_ = aptr;
}

B::~B()
{
delete aptr_;
}


OK, this is the case where setting aptr_ to NULL won't have any effect
because B (hence aptr_) is itself being destroyed. (Assuming that it
isn't B that is getting destroyed twice.)

The bug is that B::foo requires an A object that it can own, and delete,
and doesn't have a name/comment/document making that obvious to you.

No doubt you have passed the same A to multiple calls of B::foo, or
kept the A pointer and eventually deleted it, which would be the right
thing to do if B::foo did not have this undocumented requirement.
Apr 12 '06 #43
Robert Mabee wrote:
The bug is that B::foo requires an A object that it can own, and delete,
and doesn't have a name/comment/document making that obvious to you.


It would be nice we could indeed make that problem a "bug". std::auto_ptr
can only get as far as making its constructor explicit, which is as much a
nuisance as a compilable comment. ;-)

Nice thread busting on the evils of RAII, guys...

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 12 '06 #44
Victor Bazarov wrote:
Phlip wrote:
If you must, use std::auto_ptr or boost::shared_p tr, and reset(NULL)
that (if I recall correctly).


'boost' is non-standard. 'std::auto_ptr' is not suitable for some
cases due to its particular copy semantics.


No reason not to use boost. Besides, boost smart pointers have been accepted
into TR1, so they are standard and will be supported by newer compilers.

--
Paul M. Dubuc
Apr 12 '06 #45
Paul M. Dubuc wrote:
No reason not to use boost.


Also no reason not to suggest or (lightly) discuss Boost here.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 12 '06 #46

Paul M. Dubuc wrote:
Victor Bazarov wrote:
Phlip wrote:
If you must, use std::auto_ptr or boost::shared_p tr, and reset(NULL)
that (if I recall correctly).


'boost' is non-standard. 'std::auto_ptr' is not suitable for some
cases due to its particular copy semantics.


No reason not to use boost. Besides, boost smart pointers have been accepted
into TR1, so they are standard and will be supported by newer compilers.


wrong.

"This technical report is non-normative. Some of the library components
in this technical report may be considered for standardization in a
future version of C++, but they are not currently part of any C++
standard. Some of the components in this technical report may never be
standardized, and others may be standardized in a substantially changed
form."

Apr 12 '06 #47
Phlip wrote:
Paul M. Dubuc wrote:
No reason not to use boost.


Also no reason not to suggest or (lightly) discuss Boost here.


Do you apply that to all non-standard, third-pary libraries?

Brian
Apr 12 '06 #48
Default User wrote:
Also no reason not to suggest or (lightly) discuss Boost here.


Do you apply that to all non-standard, third-pary libraries?


Specific questions about some library X belong on X's forum.

The general question "what [portable] C++ library does X" belong in
the best platform-neutral C++ forum possible.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 12 '06 #49
Default User wrote:
Phlip wrote:
Paul M. Dubuc wrote:
> No reason not to use boost.


Also no reason not to suggest or (lightly) discuss Boost here.


Do you apply that to all non-standard, third-pary libraries?


Topicality, as per our FAQ, includes planned extensions to the standard. Can
you get any closer than tr1? Even though not everything in boost is up for
standardization , boost happens to be the default route of an idea into the
standard. I consider that topical.
Best

Kai-Uwe Bux

Apr 13 '06 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2344
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note that, as well as reference counting the stored pointer, it also has the ability to be declared for incomplete types (I think). I.e.: struct Incomplete;
2
1369
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded my remove function as follows template <class T>
3
9632
by: Songling | last post by:
Hi gurus, Just learnt from my co-worker that it's safe to delete a null pointer. I tried it on sun box (and linux), nothing bad happens. So it seems to be true. But does it apply to all platforms? Also if I delete a pointer twice, and don't nullify the pointer after the first delete, it's going to trap. If I do the nullify, no trap. Does it just confirm that it's safe
4
3102
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a .push_back(...) I get an "out of memory" runtime error. I know I'm not out of memory because normal vectors such as vector<int> a, still work, and still work fine.
3
3407
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; foo() {} // no public creation!
6
3023
by: KWienhold | last post by:
I'm currently working on a project in C# (VS 2003 SP1, .Net 1.1) that utilizes IStream/IStorage COM-Elements. Up to now I have gotten everything to work to my satisfaction, but now I have come across a problem I can't really explain: When deleting an object from an IStorage, the space it used up will not be freed, but rather marked as unused and overwritten the next time you add an object to the storage. This is obviously working as...
30
2751
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
6
1995
by: ma740988 | last post by:
Given the snippet. class foo { public: ~foo() { // type id should be able to get the type of the class ( I think ) std::cout << " foo destructing " << std::endl; } };
8
6130
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.