Thread: Crash in destructor

  1. #1
    Registered User
    Join Date
    Mar 2009

    Crash in destructor

    Please let me know why it is crashing in destructor:

    //Simulation of smart pointer

    class smartpointer 
    int *p;//ordinary pointer
    smartpointer(int n) 
    p =new int[n]; 
    int *t =p; 
    for(int i=0;i<n;i++) 
    int* operator++(int) 
    return p++; 
    int operator*() 
    return *p; 
    delete [] p;
    void main() 
    smartpointer sp(10);
    for(int i=0;i<10;i++) 

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    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
    so how can we delete the allocated memory in dTOR?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    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
    Join Date
    Oct 2007
    Inside my computer
    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, 07: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, 08: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