Pointers, classes, and the HEAP....Please help!!!!!

This is a discussion on Pointers, classes, and the HEAP....Please help!!!!! within the C++ Programming forums, part of the General Programming Boards category; // Listing 8.5 // Creating objects on the free store // using new and delete #include <iostream> class SimpleCat { ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Pointers, classes, and the HEAP....Please help!!!!!

    // Listing 8.5
    // Creating objects on the free store
    // using new and delete

    #include <iostream>

    class SimpleCat
    {
    public:
    SimpleCat();
    ~SimpleCat();
    private:
    int itsAge;
    };

    SimpleCat::SimpleCat()
    {
    std::cout << "Constructor called.\n";
    itsAge = 1;
    }

    SimpleCat::~SimpleCat()
    {
    std::cout << "Destructor called.\n";
    }

    int main()
    {
    std::cout << "SimpleCat Frisky...\n";
    SimpleCat Frisky;
    std::cout << "SimpleCat *pRags = new SimpleCat...\n";
    SimpleCat * pRags = new SimpleCat;
    std::cout << "delete pRags...\n";
    delete pRags;
    std::cout << "Exiting, watch Frisky go...\n";
    return 0;
    }

    /*###################################*/

    OUTPUT:
    SimpleCat Frisky...
    Constructor called.
    SimpleCat *pRags = new SimpleCat...
    Constructor called.
    delete pRags...
    Destructor called.
    Exiting, watch Frisky go...

    ###################################

    Now my question is everytime you declare a class in this case "Frisky", everytime you do this a constructor is called?
    Also what tells me here when a destructor is called?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    When deleting the object, the destructor is called.

  3. #3
    Unregistered
    Guest
    does it happen automatically?

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Only when the object has been allocated on the stack.
    zen

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    and how do I know when something it's allocated on the stack? Oh man this stack thing is confusing me so bad.
    Last edited by incognito; 12-16-2001 at 10:06 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    The destructor is called when items go out of scope.

    Not when you use the Delete keyword.

    Use the Delete keyword in the destructor.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Re: Pointers, classes, and the HEAP....Please help!!!!!

    Originally posted by elchulo2002

    Now my question is everytime you declare a class in this case "Frisky", everytime you do this a constructor is called?
    Also what tells me here when a destructor is called?
    Frisky isn't the class, SimpleCat is. Frisky is an object, an instance of the class SimpleCat. Yes, every time you declare an instance, or object, of class SimpleCat, the constructor is called.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    so everytime you delete an object the destructor is called?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  9. #9
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i am sorry to say but a lot of these informations that you got were confusing and inacurate....

    from what i see you are reading "Teach yourself C++ in 21 day" that's a good book but it's not sufficient...seems like you don't really understand classes and their components, in this case i don't suggest for you to start "dynamic memory allocation" which in this program you are doing by declaring instances of this class on the free store... i suggest you spend a lot more time with classes, and to answer your question --- Yes, every time you destroy an object of a class the destructor is called, and also in this particular program the same thing happens...there's a ton of more details when it comes to "dynamic memory managment" (new, delete) and their pointer based implementations (*pPointer)...so once again i strongly advice that you cover the "C++ Classes chapter" in great detail and then go on to more advanced stuff... i would sit here here all night trying to explain in detail to you the meaning behind your source code...

    Good Luck,
    matheo917

  10. #10
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    LITTLE BIT OF INFO....

    When a C++ program begins execution, the computerís memory is broken down into four zones that contain the programís code, all global data, the stack, and the free store (heap). The heap, or free store, is a free area of memory reserved by your program to be used at runtime; it is controlled by the programmer with new and delete. The free store is the term used in C++ and the heap was the term used in C.
    The stack is used to hold the return addresses of function calls, arguments to functions, local variables, and the current state of the CPU. If you go back to "Teach Yourself C++ in 21," I believe that there is an explanation in chapter five of the use of the stack. The other two regions, the ones that hold the program code and all global data, are self-explanatory. You can think of the free store as occupying the bottom part of the programís memory space and growing upward, while the stack occupies the top and grows downward. All this is called the memory map and I would not be too concerned with it right now. Even if you don't understand this concept right now, it won't prohibit you from continuing your training in C++.

    Regards,
    matheo917

  11. #11
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I believe I got all the classes and pointers (up to this point), I believe I got them alright....I just didn't know what happened when function was called....You know which started first and then when a function was called, what happened...you know that's all...thanks for the help though.....I guess I'll be doing some rearch.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  12. #12
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ok....
    (i thought you were actually asking about the destructor....)
    but ...whatever makes you happy...

    Good Luck...
    matheo917

  13. #13
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Yes I was also aking about the constructor and destructor, so thank you for your help, your help is greatly appreciated.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

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