vector iterator trouble (C++)
Comments added to list explaining what VS2005 (Visual Studio 2005) does:
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.
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.