C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-05-2009, 11:31 PM   #1
Registered User
 
Join Date: Mar 2009
Posts: 35
Crash in destructor

Hi,
Please let me know why it is crashing in destructor:

//Simulation of smart pointer

Code:
#include<iostream.h>

class smartpointer 
{
public: 
int *p;//ordinary pointer

public: 
smartpointer(int n) 
{
p =new int[n]; 
int *t =p; 
for(int i=0;i<n;i++) 
*t++=i*i;
}

int* operator++(int) 
{
return p++; 
}

int operator*() 
{
return *p; 
}

~smartpointer()
{
if(p)
delete [] p;
}

};

void main() 
{
smartpointer sp(10);
for(int i=0;i<10;i++) 
cout<<*sp++<<endl;
}
vaibhavs17 is offline   Reply With Quote
Old 04-05-2009, 11:59 PM   #2
Kiss the monkey.
 
CodeMonkey's Avatar
 
Join Date: Sep 2001
Posts: 810
Ensure that p is initialized to 0.
The problem, it seems, is that your ++() returns p++, which is a temporary with the value of one past the address to which p refers. Thus *p++ is problematic, since the pointer p++ might not exist anymore.
__________________
"If you tell the truth, you don't have to remember anything"
-Mark Twain
CodeMonkey is offline   Reply With Quote
Old 04-06-2009, 12:17 AM   #3
Registered User
 
Join Date: Mar 2009
Posts: 35
so how can we delete the allocated memory in dTOR?
vaibhavs17 is offline   Reply With Quote
Old 04-06-2009, 12:57 AM   #4
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,476
There are many problems with that class. Listing them from biggest to smallest problem:
  1. Postincrement causes the class to delete a different pointer to the one that was returned from new.
  2. It does not follow the rule of three.
  3. It mixes the concept of smart pointer AND iterator. These should be separate concepts.
  4. The constructor should probably be marked as explicit.
  5. It unnecessarily check p for NULL before deletion.
  6. It exposes the p member variable publicly, though this one is argueably acceptable.
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Old 04-06-2009, 11:10 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Furthermore, the code is not indented. Indent!
Plus it uses void main. Use int main.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Destructor crash Abbygail C++ Programming 1 05-25-2009 07:31 PM
Destructor being called on SGI hash_map key cunnus88 C++ Programming 4 02-11-2009 12:05 AM
exception in the destructor coletek C++ Programming 3 01-12-2009 12:01 PM
Can you call a constructor within a destructor, or vise vesa? meili100 C++ Programming 1 06-10-2008 08:38 PM
destructor question kocika73 C++ Programming 3 03-10-2006 11:29 AM


All times are GMT -6. The time now is 09:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22