This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
C++ arrays vs vectors (was: Re: signal 11 generating problem, was: Further observations regarding alloca on i586-pc-linux-gnu)
- To: <egcs at cygnus dot com>
- Subject: C++ arrays vs vectors (was: Re: signal 11 generating problem, was: Further observations regarding alloca on i586-pc-linux-gnu)
- From: "Ross Smith" <ross dot s at ihug dot co dot nz>
- Date: Thu, 27 Aug 1998 12:32:02 +1200
From: Joe Buck <jbuck@synopsys.com>
>>
>> Is auto_ptr_array a standard template? If so, where is it defined?
>
>No, there is no standard template by that name. It has been proposed
>by many people to be analogous to auto_ptr, except that it would
contain a
>pointer to an array on the heap (e.g. "new MyClass[5]") and would do
>"delete []" rather than "delete" in the destructor.
>
>The committee's response to such proposals has been to try to get
people
>to just use vector, which is OK as long as you aren't trying to
interface
>to C code that wants a genuine C array.
This comes up regularly on comp.lang.c++.moderated and comp.std.c++. A
few months ago there was a long thread about whether std::vector could
be used in place of a C array -- in other words, whether a vector's
contents were guaranteed to be contiguous in memory, so you could pass
the address of the first element to a function that expected a pointer
to a C array.
The verdict was that, strictly speaking, no, that isn't guaranteed by
the standard, but although it's possible to make a standard-conforming
vector that isn't contiguous, no real implementation is likely to do
that.
This was an oversight in the standard rather than intentional leeway for
unusual implementations. Bjarne Stroustrup (creator of C++) said that
both he and Alexander Stepanov (creator of the STL) had always thought
that vectors *were* required to be contiguous, and nobody realised until
it was too late that the wording that had gone into the final standard
didn't actually guarantee that.
Several people, including Dr Stroustrup, recommended assuming contiguous
vectors anyway, and avoiding any hypothetical weird STL implementation
that does it differently (nobody knew of any in the real world).
--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
"Remember when we told you there was no future? Well, this is it."
-- Blank Reg