The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete content in a file (delete content only) kittusri9 Shell Programming and Scripting 5 05-15-2008 01:12 PM
C++ = new and delete arun.viswanath High Level Programming 3 09-13-2005 10:16 PM
Can't delete anything in /usr ms1951 UNIX for Dummies Questions & Answers 4 10-22-2004 11:49 AM
delete lesstjm UNIX for Dummies Questions & Answers 3 11-08-2001 03:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-31-2008
nandlal nandlal is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
Post delete in c++

Hi,

do we need to set NULL to class pointer after delete it.


myClass *mC = new MyClass (a, c).

.
.
.
.
delete mC; <--- is this enough or need to set it NULL.

-nandlal
  #2 (permalink)  
Old 01-31-2008
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
My understanding is this: delete() frees the memory space allocated already. However, if you do not reset the pointer to NULL, it will continue to point to a freed memory block (which may subsequently be allocated for some other uses) and lead to segmentation fault or mysterious behaviour with unexpected value if you happen to access that pointer. To safeguard, reset the pointer to NULL as soon as the memory location is freed.
  #3 (permalink)  
Old 02-01-2008
neutrino4 neutrino4 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 2
I guess it is used for safeguarding from re-deleting some pointer that was already deleted in the past and for knowing which pointer references real object and which one is not. For example if you have some kind of a pool (array of pointers) of objects of some type that you manage manually (allocating new objects, deallocating old ones), you have to know, whether some particular pointer in the pool is actually valid. So when you deallocate an object from the pool, you set its pointer to NULL and then you know that this pointer may be used again in the future. In case of freeing the whole pool you have to delete only those pointers that are not NULL.

On my system (Ubuntu Linux 6.06) there is no difference in referencing NULLed and invalid pointer, I get segmentation fault in both cases.
  #4 (permalink)  
Old 02-01-2008
nandlal nandlal is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
thanks a lot for highlighting on my query.
  #5 (permalink)  
Old 02-02-2008
niketan niketan is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 4
Reuse of deleted pointer

As nutrino said After deleting the the class memory pointer becomes dangling pointer and there are many consequences of that and might affect to crash ur program also

I hope below article will help you

Dangling pointers
Closed Thread

Bookmarks

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0