This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Sizes when sizeof(value_type) == 1 (Was: Re: vector<> can...)


On Tue, 2004-10-19 at 01:03, Paolo Carlini wrote:
> Dhruv Matani wrote:
> 
> >Consider this line: vector.tcc => 432.
> >const size_type __len = __old_size + std::max(__old_size, __n);
> >  
> >
> Well, you see, there is something fishy going on with this kind of 
> arithmetic,
> when sizeof(value_type) == 1. The same happens in other places, also when
> __old_size * 2 is simply computed.

I don;t think so. It seems fine to me.

What's happening is the in the 1 element insert function, the storage is
multiplied by 2, while in the range insert functions, the storage is
multiplied by 2 or increased by the new required size whichever happens
to be greater.

Let new_size = old_size + new_added_elements;
Now, max(old_size*2, new_reqd_size) == old_size + max(old_size,
new_added_elements);

Of course not considering that pesky case where the vector was empty and
old_size == 0.



> 
> With max_size == size_type(-1) in that case, we are everywhere at risk of
> overflowing size_type during those computations!

Even otherwise IMHO.

We *should* have a check that __len > __old_size everywhere.

> 
> Paolo.
> 
> P.S. After all, this morning I was right in spotting something wrong...
-- 
        -Dhruv Matani.
http://www.geocities.com/dhruvbird/

The price of freedom is responsibility, but it's a bargain, because
freedom is priceless. ~ Hugh Downs


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]