Thread Closed

vector iterator trouble (C++)

 
Share Thread Thread Tools
Apr27-10, 11:52 AM   #18
 
Recognitions:
Homework Helper Homework Help

vector iterator trouble (C++)


Quote by D H View Post
Consider two cases ...
Comments added to list explaining what VS2005 (Visual Studio 2005) does:

Get_Parts_List() returns a vector<Parts>:
  • Returned value and local variable is of type vector<Parts>.
    Assuming a statement vector<Parts> parts_list = auto.Get_Parts_List(), the compiled code will
    • Allocate memory of size sizeof(vector<Parts>) for the variable,
      • VS2005 - pre-allocates memory for temporary instance of vector<Parts> on the local stack at compile time.
    • Construct this allocated memory via the vector<Parts> default constructor, and
    • Copy the auto.PartsList to parts_list via the vector<Parts> assignment operator. There will be yet more allocation, construction, and copying if the vector is not empty, one for each of the elements of the vector. Here the allocation is from the heap, not the stack.
      • VS2005 - destroys the copy after it's used. Any saved pointer would point to freed memory.

Get_Parts_List() returns a vector<Parts> &:
  • Returned value and local variable is of type vector<Parts> &.
    Assuming a statement vector<Parts> & parts_list = auto.Get_Parts_List(), the compiled code will
    • Allocate memory of the size of a pointer for the variable parts_list from the stack and
      • VS2005 - Get_Parts_List() returns the pointer in a register, EAX. Elsewhere the pointer is a pre-allocated local variable.
    • Set the contents of that memory to the address of the auto.PartsList data member.
 
Thread Closed
Thread Tools


Similar Threads for: vector iterator trouble (C++)
Thread Forum Replies
LinkedList Iterator Java tips Engineering, Comp Sci, & Technology Homework 2
Vector Trouble Calculus & Beyond Homework 3
vector trouble Precalculus Mathematics Homework 10
Calc III, Vector trouble, help would be great! Introductory Physics Homework 3
Trouble with some vector problems Introductory Physics Homework 5