C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 27th, 2005, 01:04 PM
lefnire lefnire is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 4 lefnire User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 45 sec
Reputation Power: 0
Question deleting pointers

quick newbie question:
when do I delete pointers in destructors as opposed to simply setting them to NULL?
Sometimes I (evidently wrongly) delete pointers and get the error message BLOCK_TYPE_IS_VALID(pHead->nBlocksUse)

Reply With Quote
  #2  
Old June 27th, 2005, 02:44 PM
marmo marmo is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 37 marmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 20 sec
Reputation Power: 4
Whenever you use the keyword new to allocate memory, somewhere in your code you should have a delete which counters it. By just setting your pointer to NULL in the destructor you will create a memory leak. It is good programming practice to place a safe check in your class destructors. eg.
if( pHead )
{
delete pHead;
pHead = NULL;
}

Reply With Quote
  #3  
Old June 27th, 2005, 02:55 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
Setting a pointer to NULL could be compared to "making it empty", the pointer now simply does not point anywhere.

Conversely, deleting the memory a pointer points to is freeing the memory from being restricted so that now it can be used by anything, this does not alter the pointer's value, however.

Something fun to do, depending on your OS and compiler, is to allocate memory, fill it up, then deallocate it, and view what valus it has.... sometimes it's the same, sometimes it gets filled up in a microsecond by something else
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #4  
Old July 2nd, 2005, 06:24 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
Anyone deciding to try B-Con's "something fun to do" should be advised that it will probably cause your computer to crash as the program attempts to access memory that it no longer owns...

Reply With Quote
  #5  
Old July 5th, 2005, 01:53 PM
netytan's Avatar
netytan netytan is offline
Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Knighton, UK
Posts: 29 netytan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 7 m 44 sec
Reputation Power: 0
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Forgive me if I'm wrong but accessing memory outside of the program shouldn't cause a problem to the OS, as long as you don't change the value at the location you should be good .

Even if you do change the value and something does crash, you should only crash the program. Sadly this still might crash Windows .

Take care,

Mark.

Reply With Quote
  #6  
Old July 5th, 2005, 11:54 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
Quote:
Originally Posted by netytan
Forgive me if I'm wrong but accessing memory outside of the program shouldn't cause a problem to the OS, as long as you don't change the value at the location you should be good .

Even if you do change the value and something does crash, you should only crash the program. Sadly this still might crash Windows .

Take care,

Mark.
Yeah, nothing should go wrong.... we used to do that in my C++ class just for kicks every now and then, nothing ever crashed on us

On the same note, another "fun" (but highly stupid) thing to do is to place a "ptr = new int;" line in an infinite loop, then go to the windows task manager and select the "view processes" tab and watch as the memory allocation for that program skyrockets, eventually causing the program to crash.... ah, the humor of a geek

Reply With Quote
  #7  
Old July 6th, 2005, 12:42 PM
drayel drayel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 21 drayel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 59 m
Reputation Power: 0
"ah, the humor of a geek "

Is there any other kind?

Reply With Quote
  #8  
Old July 6th, 2005, 01:20 PM
netytan's Avatar
netytan netytan is offline
Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Knighton, UK
Posts: 29 netytan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 7 m 44 sec
Reputation Power: 0
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
Originally Posted by B-Con
Yeah, nothing should go wrong.... we used to do that in my C++ class just for kicks every now and then, nothing ever crashed on us

On the same note, another "fun" (but highly stupid) thing to do is to place a "ptr = new int;" line in an infinite loop, then go to the windows task manager and select the "view processes" tab and watch as the memory allocation for that program skyrockets, eventually causing the program to crash.... ah, the humor of a geek


Hahaha I like it , will have to test some of these out just for kicks.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > deleting pointers


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway