Dynamic Memory

This is a discussion on Dynamic Memory within the C++ Programming forums, part of the General Programming Boards category; given Code: int main() { stack<MyClass *> s; MyClass *m; int i; for(i = 0; i < 5; i++) { ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Dynamic Memory

    given
    Code:
    int main()
    {
    	stack<MyClass *> s;
    	MyClass *m;
    	int i;
    	
    	for(i = 0; i < 5; i++)
    	{
    		m = new MyClass;
    		//.... some code dealing with m
    		s.push(m);
    	}
    	
    	while(!s.empty())
    		s.pop();
    	
    	return 0;
    }
    is there still 5 MyClasses's on the heap?

    in other words, will the stack (container) clean up for me or not?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    is there still 5 MyClasses's on the heap?
    Yes, the stack container only manages the memory needed to store the addresses of the MyClass objects. Whether the addresses are heap or stack addresses, it doesn't care.

    in other words, will the stack (container) clean up for me or not?
    No, you will have to delete the MyClass objects yourself.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Make simple test like this one:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    class MyClass
    {
          public:
                 MyClass()
                 {
                          cout<<"Costructor"<<endl;
                 }
                 ~MyClass()
                 {
                           cout<<"Dectructor!"<<endl;
                 }
                 
    };
    
    int main()
    {
    	stack<MyClass *> s;
    	MyClass *m;
    	int i;
    	
        for(i = 0; i < 5; i++)
    	{
    		m = new MyClass;
    		//.... some code dealing with m
    		s.push(m);
    	}
    	
    	while(!s.empty())
    		s.pop();
    
    	system("pause");
    	return 0;
    }
    As you can see only constructors are called, but none destructor, that simply means object are created (on heap by calling operator new) but not destroyed.
    Hope this helps.
    Cheers!

    Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 10:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21