This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: ISO C++ forbids initialization in array new?
Joe Buck <Joe.Buck@synopsys.COM> writes:
> On Fri, Aug 19, 2005 at 09:19:55AM +0100, Jonathan Wakely wrote:
>> I assume you are trying something like this:
>>
>> int* i = new int[5](23);
>
> While this is not gcc-help, maybe we could make a FAQ item for this.
> For people who want to do something like this, I suggest
>
> std::vector<int> i(5, 23);
>
> If the reason vector wasn't used in the first place was because some
> API needs an array, then use
>
> std::vector<int> vector_i(5, 23);
> int* i = &*vector.begin();
This is not quite the same, because when vector_i goes out of scope it
will be destructed, so you can't use the pointer beyond that point.
Andreas.
--
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."