Delete keyword and Destructor problem

This is a discussion on Delete keyword and Destructor problem within the C++ Programming forums, part of the General Programming Boards category; Hi.. I have misunderstanding with delete keyword, specially with data structures programming, in the following code, does i have to ...

  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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    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;
        }	
    };
    I never put signature, but I decided to make an exception.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    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

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