User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
notes:stl_allocation_example [2013/02/25 16:58]
notes:stl_allocation_example [2013/02/25 17:06]
andy [Typical Output]
Line 89: Line 89:
 ==== Typical output ==== ==== Typical output ====
 +The output below demonstrates that an instance is first constructed in-place on the stack (the expression ''​MyClass(11)''​ does this, for example). Then, the ''​std::​list''​ code uses [[wikipedia>​placement new]] to copy the item into place, which results in the copy constructor being invoked (assuming that placement new itself isn't overridden). Finally, the destructor of the in-place instance on the stack is invoked.
 <​file>​ <​file>​
Line 224: Line 226:
 ==== Typical Output ==== ==== Typical Output ====
 +In contrast with the [[#​std::​list]] example above, ''​std::​map''​ invokes a substantial number of copy operations in the process of adding an element via assignment, as is done above.
 +First, an in-place instance is constructed on the stack in response to the expression ''​MyClass(11)''​ as was done for ''​std::​list''​. Next, the default constructor is called to instantiate the instance in the map on the left-hand side of the assignment, and [[wikipedia>​placement new]] is used to copy this into the map --- I'm currently not sure why there appears to be yet another additional temporary value used in the process. At this point the destructors of both temporary instances are invoked. Then the assignment operator is used to overwrite the value in the map with the in-place instance created earlier. Finally, the destructor for the in-place instance is called, as with ''​std::​list''​.
 <​file>​ <​file>​
notes/stl_allocation_example.txt ยท Last modified: 2013/02/25 17:31 by andy