Crash in destructor

This is a discussion on Crash in destructor within the C++ Programming forums, part of the General Programming Boards category; Hi, Please let me know why it is crashing in destructor: //Simulation of smart pointer Code: #include<iostream.h> class smartpointer { ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    40

    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;
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    so how can we delete the allocated memory in dTOR?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Furthermore, the code is not indented. Indent!
    Plus it uses void main. Use int main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor crash
    By Abbygail in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2009, 08:31 PM
  2. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2009, 12:05 AM
  3. exception in the destructor
    By coletek in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2009, 12:01 PM
  4. Replies: 1
    Last Post: 06-10-2008, 09:38 PM
  5. destructor question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2006, 11:29 AM

Tags for this Thread


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