std::vector::_Reserve() contains a suboptimal check against max_size - by Dmitry Me

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 694881 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 10/13/2011 11:55:18 PM
Access Restriction Public


As of version 10.0.40219.1 SP1Rel std::vector::_Reserve() code goes like this:

if (max_size() - _Count < _Size)
else if ((_Size += _Count) <= capacity())

Now capacity() <= max_size() at all times. Which means that if capacity() >= _Size + _Count (enough room) then automatically max_size() >= _Size + _Count. Which means that whenever that code is invoked when a vector already has room (that's quite often) the check max_size() >= _Size() + _Count is redundant.

_Reserve() should be redesigned like this:

if (capacity() - _Size >= _Count)
;//enough room - do nothing
else if( (max_size() - _Size < _Count)
     reserve(_Grow_to( _Size+_Count));

so that the check is done only if there's a need for reallocation in the first place.
Sign in to post a comment.
Posted by Microsoft on 11/4/2011 at 9:15 PM

Thanks for suggesting this micro-optimization. We've implemented it, "flipping" the _Xlen() check, and it will be available in VC11.

All of _Insert(), _Insert_n(), _Reserve(), and reserve() have been changed. I hope you don't mind if I copy and paste this response for your four bugs - in the future, please consider filing a single bug when a single issue appears in multiple places.

If you have any further questions, feel free to E-mail me at .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator10 [Feedback Moderator] on 10/14/2011 at 2:15 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 10/14/2011 at 12:45 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(