vector<> can probably never grow to it's maximum size!

Dhruv Matani dhruvbird@gmx.net
Tue Oct 19 17:14:00 GMT 2004


This patch seems more correct, because it handles the case where the
memory in fact can be allocated.

-Dhruv.

On Tue, 2004-10-19 at 22:04, Dhruv Matani wrote:
> On Tue, 2004-10-19 at 20:44, Paolo Carlini wrote:
> > Dhruv Matani wrote:
> > 
> > >And tries to explain more concretely what I'm trying to achieve and also
> > >shows that it's do able in current C++.
> > >
> > Why you keep on refusing to deal with one issue at a time?
> 
> I know :-( I have to try and consciously stick to one thing at a time,
> and learn to control my frivolous nature.
> 
> > 
> > The below cannot fix, not even in principle, the immediate problem that
> > we have with __len overflowing size_type: all the new tricks in __mt_alloc
> > are not in effect in this case, ::operator new for sure will never return
> > an address == _M_start (half the memory is already used), it will always
> > throw, even if you ask only for __old_size + 1. 
> 
> Ah! I did not think about this. You are right on this count because
> vector first allocates and then deallocates, so no matter what if half
> the memory is used, you are doomed.
> 
> > Don't try to achieve the
> > impossible, concentrate on the case of small memory allocations, managed
> > via mt_alloc memory pools: in that case there is rooom for improvement..
> 
> Yes, I have shown in the patch that the small size cases can be dealt
> with quite well with __mt_alloc<>.
> 
> 
> Attached is a patch and test-case which shows that the patch actually
> works.
> 
> > 
> > Paolo.
-- 
        -Dhruv Matani.
http://www.geocities.com/dhruvbird/

The price of freedom is responsibility, but it's a bargain, because
freedom is priceless. ~ Hugh Downs
-------------- next part --------------
--- vector.tcc	2004-08-23 17:48:26.000000000 +0530
+++ /home/dhruv/projects/temp/vector.tcc	2004-10-19 22:25:15.000000000 +0530
@@ -263,6 +263,9 @@ namespace _GLIBCXX_STD
 	{
 	  const size_type __old_size = size();
 	  const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
+	  if (__len < __old_size)
+	    __throw_length_error(__N("vector::_M_insert_aux"));
+
 	  iterator __new_start(this->_M_allocate(__len));
 	  iterator __new_finish(__new_start);
 	  try
@@ -337,7 +340,17 @@ namespace _GLIBCXX_STD
 	  else
 	    {
 	      const size_type __old_size = size();
-	      const size_type __len = __old_size + std::max(__old_size, __n);
+	      size_type __len = __old_size + std::max(__old_size, __n);
+	      if (__len < __old_size)
+		if (__n < __old_size)
+		  {
+		    __len = __old_size + __n;
+		    if (__len < __old_size)
+		      __throw_length_error(__N("vector::_M_fill_insert"));
+		  }
+		else
+		  __throw_length_error(__N("vector::_M_fill_insert"));
+
 	      iterator __new_start(this->_M_allocate(__len));
 	      iterator __new_finish(__new_start);
 	      try
@@ -429,7 +442,17 @@ namespace _GLIBCXX_STD
 	    else
 	      {
 		const size_type __old_size = size();
-		const size_type __len = __old_size + std::max(__old_size, __n);
+		size_type __len = __old_size + std::max(__old_size, __n);
+		if (__len < __old_size)
+		  if (__n < __old_size)
+		    {
+		      __len = __old_size + __n;
+		      if (__len < __old_size)
+			__throw_length_error(__N("vector::_M_range_insert"));
+		    }
+		  else
+		    __throw_length_error(__N("vector::_M_range_insert"));
+		
 		iterator __new_start(this->_M_allocate(__len));
 		iterator __new_finish(__new_start);
 		try


More information about the Libstdc++ mailing list