473,387 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

safe to delete null pointer?

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
to delete null pointer?

Btw I ran my test on SunOS 5.8 and Linux 2.4.18-3.

Thanks for your reply!

Songling
Jul 22 '05 #1
3 9589
Songling wrote:
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
to delete null pointer?

Btw I ran my test on SunOS 5.8 and Linux 2.4.18-3.

Thanks for your reply!

Songling

Calling delete on a null pointer is a no-op -- and, by the standard, no
problem. Of course, the fact that you're deleting a null pointer may
indicate the existence of a bug in your program -- but not necessarily.

HTH,
--ag

--
Artie Gold -- Austin, Texas

http://it-matters.blogspot.com http://www.cafepress.com/goldsays
Jul 22 '05 #2
Songling wrote:
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
to delete null pointer?

Deleting a null pointer is safe. 5.3.5/2 of the Standard:

"In either alternative, if the value of the operand of delete is the
null pointer the operation has no effect."

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 22 '05 #3
"Songling" <ya******@nortelnetworks.com> wrote in message news:<co**********@zcars0v6.ca.nortel.com>...
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
to delete null pointer?

Btw I ran my test on SunOS 5.8 and Linux 2.4.18-3.

Thanks for your reply!

Songling


Deleting a null poiner is fine. It will have no effect and will behave as expected
Jul 22 '05 #4

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

Similar topics

2
by: qazmlp | last post by:
Say, ptr=0 / ptr=NULL. delete ptr; //is safe // Is delete ptr; // also safe ?
21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
2
by: Christopher Pisz | last post by:
currently my node is like so with everything public: class Quadtree_Node { public: Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds); ~Quadtree_Node(); void Subdivide(float...
11
by: Squid Seven | last post by:
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...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
5
by: Felix Kater | last post by:
Hi, if I want to make sure that neither the pointer arg nor the value to which it points to is NULL: Is solution (a) safe -- or does it have to be like (b) ? Felix void f(int* p){ /* (a)...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.