Thread: delete pointer inside ?

  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~

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    Not necessarily. If you fail to release the memory pointed to by the pointer before the object is destroyed then yes, you'll have a memory leak. But that alone won't crash your machine unless you create and destroy large numbers of objects of this class in a program that runs for long periods of time.

    If you only create and destroy a few objects then the leak won't be bad and you'll have no ill effect to the rest of the program unless it uses a large amount of memory elsewhere and those few bytes are critical later in the execution. If the program doesn't run for a long time then when it terminates, the operating system (assuming a hosted implementation that actually does this) will reclaim the memory used by your process and all will be well.

    Still, it's a good idea to clean up after yourself because useful programs that weren't meant to be run often or for long periods of time tend to be reused as libraries in applications that do.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    "I want to delete a pointer" is a common way to say "I want to delete the memory a pointer points to". But, I think it's important to point out that we don't delete pointers, we delete memory that we declare. We keep track of memory that we declare by using pointers, but deleting the memory is not the same as deleting the pointer.

    //declare pointer to int
    int * p;

    //show memory address of p
    cout << &p << endl;

    delete p; //error in logic, if not otherwise

    //declare memory and assign memory address to p
    p = new int;

    //show memory address of p is different than memory address pointed to by p
    cout << &p << endl;
    cout << p << endl;

    //delete memory p points to
    delete p;

    //show that memory address of p is not deleted by the above call to delete
    cout << &p << endl;

    Given the OP, I'm not sure the author is comfortable with the distinction.

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