Thread: Delete keyword and Destructor problem

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    14

    Delete keyword and Destructor problem

    Hi..
    I have misunderstanding with delete keyword, specially with data structures programming, in the following code, does i have to delete the dynamic prt, or it will destruct automatically,
    Please explain what is happen??
    and thank you in advance
    Code:
    #include "iostream"
    using namespace std;
    template <class T>
    class A
    { 
    private:int *ptr;
    public:
    	
    	A(){
    	int *ptr=new int(9);
    	cout<<"\nAAA\n";
    	}
    	 ~A()
    	{
    		cout<<"D "<<" ";
    		delete ptr;
    	}
    	
    };
    void main()
    {	A<int> a11;	
    }
    "this code doesn't work properly"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No such thing in C++ as void main, of course.

    Anything you new you must delete. The fun part is that you probably think that this line:
    Code:
    int *ptr = new int(9);
    has something to do with your private ptr variable. It doesn't.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The problem is, you redeclare your int *ptr inside your ctor.
    This makes your class member variable invisible (and uninitialised).
    So when you come to delete it, it's garbage.

    Code:
    $ g++ -Wall foo.cpp
    foo.cpp: In constructor ‘A<T>::A() [with T = int]’:
    foo.cpp:21:   instantiated from here
    foo.cpp:10: warning: unused variable ‘ptr’
    Just do
    Code:
    ptr=new int(9);
    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.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I'm wondering why you templated this class. You might want to do:

    Code:
    template <class T>
    class A
    { 
    private:
        T *ptr;
    public:
        A()
        {
            ptr=new T(9);
    	cout<<"\nAAA\n";
        }
        ~A()
        {
            cout<<"D "<<" ";
    	delete ptr;
        }	
    };

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <memory>
    
    template <typename T>
    class A
    { 
    private:
        std::shared_ptr<T> m_ptr;
    public:
        A()
        {
            m_ptr = std::make_shared<T>(9);
    	std::cout << "\nAAA\n";
        }
        ~A()
        {
            std::cout << "D " << " ";
        }	
    };
    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