strange problem about delete

This is a discussion on strange problem about delete within the C++ Programming forums, part of the General Programming Boards category; Hi, here is the code: Code: #include <iostream.h> class A { private: int i; public: void test(){cout<<"test"<<endl;} }; int main() ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    strange problem about delete

    Hi, here is the code:

    Code:
    #include <iostream.h>
    class A
    {
    private:
    	int i;
    	public:
    	void test(){cout<<"test"<<endl;}
    
    };
    
    
    
    
    
    int main()
    {
    
      A* pt = new A;
       delete pt;
      pt = NULL;
      cout<<pt<<endl;
      pt->test();
      return 1;
    }
    The pointer pt still call the function even it is deleted, what is reason? Thanks
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > The pointer pt still call the function even it is deleted, what is reason?
    Because the compiler turns it into
    A::test(pt);

    So long as you don't try and access any members of the class, I doubt there would be a problem.
    The 'this' pointer which the class member function would see would be the NULL pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    The statement delete pt; deletes the object of the class it points to, however it's still a pointer to the a class obj (whether it exists or not). Class member functions still exist, but the object and its data members are deleted.

    Compile this and see what it does...
    Code:
    #include <iostream>
    using namespace std;
    
    class TestClass
    {
        public:
            TestClass();
            ~TestClass();
            void test(){ cout << "test " << i << endl; }
            
        private:
            int i;
    };
    
    TestClass::TestClass()
        : i(24)
    {
        cout << "Constructor called" << endl;
    }
    
    TestClass::~TestClass()
    {
        cout << "Destructor called" << endl;
    }
    
    int main()
    {
        cout << "initializing TestClass *objPtr = new TestClass; instantiates objPtr" << endl;
        TestClass *objPtr;
        objPtr = new TestClass;
        
        cout << "\nobjPtr->test(); calls member function from TestClass before delete" << endl;
        objPtr->test();
        
        cout << "\ndeleting objPtr destroys object" << endl;
        delete objPtr;
        
        objPtr = NULL;
        cout << "\ncout << objPtr << endl; verifies pointer set to NULL" << endl;
        cout<< objPtr <<endl;
        cout << "\nobjPtr->test(); calls member function from deleted TestClass obj" << endl;
        objPtr->test();
        
        return 0;
    }
    Last edited by Scribbler; 02-03-2005 at 03:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 10:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 03:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Extremely strange "delete" problem
    By dr_jaymahdi in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 10:06 PM
  5. strange problem with dev cpp
    By henrychan22 in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2006, 12:05 PM

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