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

Dhruv Matani dhruvbird@gmx.net
Tue Oct 19 16:46:00 GMT 2004


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 --------------
A non-text attachment was scrubbed...
Name: vector_grow.cpp
Type: text/x-c++
Size: 287 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20041019/82e9b69d/attachment.bin>
-------------- next part --------------
2004-10-19  Dhruv Matani  <dhruvbird@gmx.net>
	
	* bits/vector.tcc: vector::_M_insert_aux, vector::_M_fill_insert,
	vector::_M_range_insert: Insert a check to see if the new size of
	the vector is less than the old size. If so, throw
	std::length_error.
	
-------------- next part --------------
--- vector.tcc	2004-08-23 17:48:26.000000000 +0530
+++ /home/dhruv/projects/temp/vector.tcc	2004-10-19 20:50:01.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
@@ -338,6 +341,9 @@ namespace _GLIBCXX_STD
 	    {
 	      const size_type __old_size = size();
 	      const size_type __len = __old_size + std::max(__old_size, __n);
+	      if (__len < __old_size)
+		__throw_length_error(__N("vector::_M_fill_insert"));
+
 	      iterator __new_start(this->_M_allocate(__len));
 	      iterator __new_finish(__new_start);
 	      try
@@ -430,6 +436,9 @@ namespace _GLIBCXX_STD
 	      {
 		const size_type __old_size = size();
 		const size_type __len = __old_size + std::max(__old_size, __n);
+		if (__len < __old_size)
+		  __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