Thread: Pointers to objects on the heap

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Pointers to objects on the heap

    Hello all! I'm teaching myself C++ but am having a little trouble understanding pointers to objects. Take this class:

    Code:
    class Car {
        public:
            Car();
            ~Car();
        private:
            int * val1;
            int * val2;
    };
    
    Car::Car() {
        int val1 = new int(5);
        int val2 = new int (7);
    }
    
    Car::~Car() {
        delete val1;
        delete val2;
    }
    
    int main() {
        Car Ford;
        return 0;
    }
    This I understand. Ford is a variable local to main, which is created on the stack, and holds two pointers to ints which are on the heap (val1 and val2).

    Now onto the confusing bit:

    Code:
    class Car {
        public:
            Car();
            ~Car();
        private:
            int val1;
            int val2;
    };
    
    Car::Car() {
        int val1 = 5;
        int val2 = 7;
    }
    
    Car::~Car() {}
    
    int main() {
        Car * Ford = new Car;
        delete Ford;
        return 0;
    }
    Isn't this the same thing? Isn't Ford a variable local to main which holds the addresses of two ints stored on the heap? That can't be right, which is why I'm confused :P.

    Thanks for your help.
    Last edited by foot; 07-21-2005 at 07:45 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int val1 = new int(5);
    > int val2 = new int (7);
    You already declared these, so this should be:
    val1 = new int(5);
    val2 = new int (7);

    > int val1 = 5;
    > int val2 = 7;
    Same here.

    >Isn't this the same thing?
    Pretty much. A picture of the first Ford object in memory would look like:
    Code:
    Ford                 heap
    ------------      ------------
    |          |----> |  val1=5  |
    ------------      ------------
    
    ------------      ------------
    |          |----> |  val1=7  |
    ------------      ------------
    The second Ford object in memory would look like:
    Code:
                         heap
                      Ford
                      ------------
    ------------      |  val1=5  |
    |          |----> ------------
    ------------      ------------
                      |  val2=7  |
                      ------------

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    Oops sorry about that mistake :P.

    The ASCII diagram really helped! The confusion stemmed from the fact that the Ford pointer actually holds two addresses, and thus I thought of it as the actual class, instead of a pointer which until now I had only used with the built-in types.

    So in a 3rd example, a pFord which pointed to a Car class Ford with two pointers to ints would look like:

    Code:
                         heap
                      Ford
                      ------------      ------------
    ------------      |  pVal1   |----> |   *pVal1 |
    |          |----> ------------      ------------
    ------------      ------------      ------------
                      |  pVal2   |----> |   *pVal2 |
                      ------------      ------------
    Thank you so much for clarifying this for me .
    Last edited by foot; 07-21-2005 at 09:59 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by foot
    Thank you so much for clarifying this for me .
    Glad to help. For completeness I should mention the Car member functions are also stored separately in memory. I haven't studied this , but I think generally there are code sections for the code, and data sections for the data (shown in our diagrams). The C++ compiler would handle this stuff.
    Last edited by swoopy; 07-21-2005 at 12:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  3. objects and pointers
    By SiteResources in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2003, 08:29 AM
  4. Inheritance and Arrays
    By codegirl in forum C++ Programming
    Replies: 18
    Last Post: 06-06-2003, 12:46 PM
  5. Creating Objects On The Heap And Adding Them To A List
    By SomeBeginner in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 04:01 PM