Thread: delete pointer inside ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    delete pointer inside ?

    howdy~

    we can delete pointer like
    Code:
    delete pt;
    and what about if a pointer has another pointer inside ? like code below:
    Code:
    class Test
    {
        private:
            int* pt;
    };
    int main()
    {
        Test* t;
        delete t;
    }
    when the time we delete t, can the pointer pt inside t also be removed ?
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    t is just a pointer to a test object. Your code does not tell t to point to any object. If you wrote Test * t = Test_Object (Where Test_Object was already defined in your code) deleting it should not delete the object.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    public:
      A(){cout<<"A constructor"<<endl; data=new int;}
      ~A(){cout<<"A destructor"<<endl; delete data; }
    private:
     int* data;  
    
    };
    
    int main()
    {
    A* aObj=new A;
    cout<<"Deleting aObj"<<endl;
    delete aObj;
    cin.get();
    
    }
    The class destructor should handle any deallocation of private data members. When you use delete on a class it will call its destructor.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by JaWiB
    The class destructor should handle any deallocation of private data members. When you use delete on a class it will call its destructor.
    ie, the pointer inside will persist all the way if we didnt free it in class's constructor ?
    Never end on learning~

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Deleting the pointer to the allocated object will call that objects destructor. If the object itself has allocated any memory, the destructor has to delete this memory, or it will float around forever.

    All memory allocated inside the class should be deleted in the destructor.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by nvoigt
    Deleting the pointer to the allocated object will call that objects destructor. If the object itself has allocated any memory, the destructor has to delete this memory, or it will float around forever.

    All memory allocated inside the class should be deleted in the destructor.
    so if we forget to delete the pointer whin the body of Test's destructor will result in lack of memory and crash our machine, didnt i go wrongly here ?
    Code:
    class Test
    {
        public:
            Test(int n) { pt = new int(n) };
            ~Test( delete pt; );
        private:
            int* pt;
    }
    anyway thanx guys for all reply~
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a class inside the WindowsProcedure function
    By like_no_other in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2009, 12:52 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. delete on a NULL pointer
    By myname in forum C++ Programming
    Replies: 18
    Last Post: 10-03-2003, 08:23 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM