+ Reply to Thread
Results 1 to 2 of 2

Thread: Delete keyword in C++? Is it safe to delete NULL pointer?

  1. #1

    Delete keyword in C++? Is it safe to delete NULL pointer?

    Hi all,

    What is the purpose of delete keyword?
    Is it safe to delete NULL pointer?

    Regards.

  2. #2
    Hi,

    a. 'delete' keyword is used to destroy an object which is created using 'new' keyword.
    b. Remember that delete automatically calls "destructor of the class.
    c. Destructor is special method in the class having "~" as prefix. For example "~CDelete" is destructor for CDelete class. See below example.
    d. Destructor allows user to cleanup the memory its class has allocated.

    Example:

    Code:
    class CDelete
    {
    public:
    	CDelete()
    	{
    		printf("CDelete") ;
    	}
    
    	~CDelete()
    	{
    		printf("~CDelete") ;
    	}
    } ;
    
    void UseDelete()
    {
    	CDelete *pDelete = new CDelete() ;
    
    	delete pDelete ;
    	pDelete = NULL ;
    
    
    	//Yes, it is safe to delete NULL pointer as per C++ standard says!
    	delete pDelete ;
    }
    Thanks.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts