473,715 Members | 4,299 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 #1
51 10429
Joe Van Dyk wrote:
When you delete a pointer, you should set it to NULL, right?


No. Your pointer should always be about to go out of scope, so don't bother
to NULL it.

If you must, use std::auto_ptr or boost::shared_p tr, and reset(NULL) that
(if I recall correctly).

Then their secret internal pointer will delete just before they go out of
scope, and they will take care of its NULL.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 10 '06 #2
Joe Van Dyk wrote:
When you delete a pointer, you should set it to NULL, right?


No. If you delete a pointer, it's probably a class member, and you're
deleting it from a destructor. In that case, you don't set it to 0,
because
noone will ever notice. The other common case is in assignments, but
in that case you will have new'ed a replacement object before. (You do
the new first, so if you run out of memory the assignment fails but the
old value is not lost).

Of course, if you use a smart pointer this is all automatic.

HTH,
Michiel Salters

Apr 10 '06 #3
Phlip wrote:
Joe Van Dyk wrote:
When you delete a pointer, you should set it to NULL, right?
No. Your pointer should always be about to go out of scope, so don't
bother to NULL it.


Nonsense. If my pointer is a data member and I am managing my own
memory, I should set it to null pointer in some cases. It doesn't
necessarily go out of scope right after deletion.
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.
[..]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '06 #4
Mi************* @tomtom.com wrote:
Joe Van Dyk wrote:
When you delete a pointer, you should set it to NULL, right?
No. If you delete a pointer, it's probably a class member, and you're
deleting it from a destructor.


And if he's not?
In that case, you don't set it to 0,
because
noone will ever notice. The other common case is in assignments, [..]


What if the case *is* uncommon?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '06 #5
On Mon, 10 Apr 2006 13:58:52 GMT, Joe Van Dyk <jo********@boe ing.com>
wrote:
When you delete a pointer, you should set it to NULL, right?


You don't HAVE to.

But, while others here obviously disagree (I did read some of the
other replies), I say that you should get in the habit of always
setting pointer to NULL after you delete them, whether or not that has
any real effect (e.g. if it's about to go out of scope then NULL'ing
it has no effect but is still a good habit to get into).

It doesn't hurt and is often helpful.
Apr 10 '06 #6

C. J. Clegg wrote:
On Mon, 10 Apr 2006 13:58:52 GMT, Joe Van Dyk <jo********@boe ing.com>
wrote:
When you delete a pointer, you should set it to NULL, right?


You don't HAVE to.
doesn't hurt and is often helpful.


And there it is. There is no reason NOT to beyond linespace fetishes.
It can save from hours to days debugging. In fact, if you set your
pointers to 0 all the time then some bugs will never bite you that
otherwise would have taken hours or days to debug. Deleting a 0
pointer is not an error, deleting any other pointer that has been
deleted already is and can be one of the most difficult types of errors
to track down.

It is one minor step you can use to help you keep bugs out of your
programs. Scope isn't the issue, getting in the habit of ALWAYS doing
it benefits you in numerous ways.

Apr 10 '06 #7
Joe Van Dyk wrote:
Victor Bazarov wrote:
Mi************* @tomtom.com wrote:
Joe Van Dyk wrote:

When you delete a pointer, you should set it to NULL, right?
No. If you delete a pointer, it's probably a class member, and you're
deleting it from a destructor.


And if he's not?

I'm not, actually.

Let's see if I can explain what's going on...

I've got an object (Object A) that contains two pointers to objects that
Object A didn't create.

There's another object in the system, Object B. Object B has a function
that requires an Object A pointer.

Object B gets deleted. It is (and out of my control) deleting the
Object A pointer. It's not setting the Object A pointer to NULL.

Now, there's threads involved here, so that may be complicating
things... but I was curious as to if the Object A pointer should be set
to NULL when Object B deletes it.

<snip>

Joe


Oh, and looking at the backtrace on the segfault, it's occuring right as
Object B deletes the pointer to Object A.

And right before I call that Object B function, I'm creating a new
Object A instance and sending a pointer to that object to Object B, if
that makes a difference.

I hope I explained that ok. I'm getting confused just thinking about
it. I'm not terribly smart, you see.

Apr 10 '06 #8
Victor Bazarov wrote:
Mi************* @tomtom.com wrote:
Joe Van Dyk wrote:
When you delete a pointer, you should set it to NULL, right?


No. If you delete a pointer, it's probably a class member, and you're
deleting it from a destructor.

And if he's not?


I'm not, actually.

Let's see if I can explain what's going on...

I've got an object (Object A) that contains two pointers to objects that
Object A didn't create.

There's another object in the system, Object B. Object B has a function
that requires an Object A pointer.

Object B gets deleted. It is (and out of my control) deleting the
Object A pointer. It's not setting the Object A pointer to NULL.

Now, there's threads involved here, so that may be complicating
things... but I was curious as to if the Object A pointer should be set
to NULL when Object B deletes it.

<snip>

Joe
Apr 10 '06 #9
Joe Van Dyk wrote:
[..]
Let's see if I can explain what's going on...

I've got an object (Object A) that contains two pointers to objects
that Object A didn't create.
So something like

class Other;
class A {
Other *not_mine_1, *not_mine_2;
public:
~A() {} // do nothing to pointers
};
There's another object in the system, Object B. Object B has a
function that requires an Object A pointer.
class B {
public:
void foo(A* aptr);
};
Object B gets deleted. It is (and out of my control) deleting the
Object A pointer.
Which A pointer? You didn't say that B _owned_ a dynamic A object.
It's not setting the Object A pointer to NULL.
Shouldn't be a problem _even_if_ the B actually owned that A and had
a pointer to that dynamic object.
Now, there's threads involved here, so that may be complicating
things... but I was curious as to if the Object A pointer should be
set to NULL when Object B deletes it.


Well, no. Your problem is most likely is elsewhere. Read about "The
Rule of Three" and follow it (concerning the B class). Also, make sure
you're not deleting that B object _twice_ somewhere.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '06 #10

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

Similar topics

2
2341
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
1363
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
9626
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
3100
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
3405
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
3019
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
2735
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
6
1990
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
6122
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
8823
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
9343
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
9104
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
9047
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
7973
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...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4477
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...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
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.