POV-Ray : Newsgroups : povray.off-topic : C++: Seriously? : Re: C++: Seriously? Server Time
28 Jul 2024 16:28:50 EDT (-0400)
  Re: C++: Seriously?  
From: Warp
Date: 22 Nov 2013 11:32:43
Message: <528f872b@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> So there is literally only one possible implementation for vector?

Not really. For example, the standard doesn't make guarantees about
when exactly reallocation happens when you keep adding elements to
the vector, or how much the capacity is increased each time it happens.
That's up to the implementation. (The only thing that the standard
guarantees is that adding new elements at the end of the vector is
amortized-constant-time.)

It also doesn't give promises about where the allocated memory will be.
(For example, it would be perfectly possible for a std::vector
implementation to use a "short vector optimization", which would be
similar to the concept of "short string optimization". All this would
be completely transparent to the outside.)

The only thing it does guarantee is that the memory allocated by
std::vector will be contiguous (like with 'new[]'), and a pointer
to one of its elements will work the same as a pointer to an element
in a raw array.

(Note that if you add a new element to a std::vector, any existing
iterators and pointers to it are invalidated. This is because if
reallocation happens, they will end up pointing to freed memory,
and thus using them after that is a no-no.)

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.