Thread: help me understand the stack

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    188

    help me understand the stack

    i'm trying to get a better understand of differences between the stack and the heap, especially since RAII is very important in C++. please see the following code:

    Code:
    static int Creation;
    static int Deletion;
    struct Test
    {	
    	int Value;
    	Test() { ++Creation; }
    	~Test() { ++Deletion; }
    };
    int main()
    {
    	std::vector<Test> list;
    	for (int i = 0; i < 10; ++i)
    	{
    		Test t;
    		t.Value = i;
    		list.push_back(t);
    	}
    }
    ok, pretty simple code. however, Creation = 10, and Deletion = 42 after that loop is finished. i'm assuming that the push_back function copies data into the list, otherwise all the data would be gone after the loop exits...but why is the constructor never called more than the 10 times, and so many deletions?

    thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but why is the constructor never called more than the 10 times, and so many deletions?
    The copy constructor is called, but you only track the default constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    but why [...] so many deletions?
    Because every constructed object has to be destroyed. But then, why so many objects has been constructed ?

    Well, the number of object constructed in this case is implementation dependent. That's because of how vector works; it reserve some memory for a particular number of objects, and when the limit is reached, it gets a larger piece of memory and copy every object (using the copy constructor) from the first piece of memory to the newly allocated second piece of memory and destroy the object from the first piece of memory before releasing the memory, etc etc (well, at least that's what happens for you and me). If you use a std::list<Test> instead of a std::vector<Test>, you should have more "reasonable" numbers.

    Also, after the loop, the object in the container has been created but not destroyed. If you want to destroy them before the end of main, call the clear member function. Then, you'll get an equal numbers of constructions and deletions.

    Oh and by the way, I'm not sure why you are talking about RAII. Indeed, I'm not even sure to understand why you are talking about stack and heap.
    I hate real numbers.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Try this version and see what you get:
    Code:
    struct Test
    {	
    	int Value;
    	Test() { ++Creation; }
    	Test( const Test&  t ) { ++Creation; }
    	~Test() { ++Deletion; }
    };
    Last edited by cpjust; 08-25-2008 at 01:28 PM. Reason: Removed operator=()

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    a = b doesn't create a new instance, though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    a = b doesn't create a new instance, though.
    Oh, that's right.
    Fixed.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    thanks for the replies. it makes a lot more sense now.


    Quote Originally Posted by foxman View Post
    Oh and by the way, I'm not sure why you are talking about RAII. Indeed, I'm not even sure to understand why you are talking about stack and heap.
    i'm trying trying to understand the mechanics of how memory works so i can write better more efficient code. i hadn't thought about the copy constructor at all, but now i can take that into consideration when i'm coding.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by anon View Post
    a = b doesn't create a new instance, though.
    huh ???but A(B b) is usually equivalent to a = b.I mean is implementation dependant right?
    Or are you talking about vector...

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but A(B b) is usually equivalent to a = b.
    If you mean that a constructor for A that constructs an A object using a B object as its only argument, then yes, such a constructor can be called with syntax that involves the = symbol unless it is declared explicit:
    Code:
    A a = b; // b is of type B.
    But the above is different from:
    Code:
    a = b; // a and b are of type A
    which is copy assignment, and that is what is in question here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by laserlight View Post
    If you mean that a constructor for A that constructs an A object using a B object as its only argument, then yes, such a constructor can be called with syntax that involves the = symbol unless it is declared explicit:
    Code:
    A a = b; // b is of type B.
    But the above is different from:
    Code:
    a = b; // a and b are of type A
    which is copy assignment, and that is what is in question here.
    Yeah I actually mean a and b is of type of A.I made a mistake since I always uses A a and B b when writing 'toy' code....
    hehe Actually,I was about to correct my mistake before your post,but I'm at work and as I was in the process of deleting that careless post,in corner of my eyes I saw my co-worker(actually he's group leader or something like that) coming towards me.I quicky kill the internet browser and pretending I'm hard at work :P

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by bling View Post
    thanks for the replies. it makes a lot more sense now.



    i'm trying trying to understand the mechanics of how memory works so i can write better more efficient code. i hadn't thought about the copy constructor at all, but now i can take that into consideration when i'm coding.
    Actually, don't get too twisted up about it. Write robust, clean code first. If it turns out not to perform well, profile it and fix the bottlenecks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

Tags for this Thread