This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: error: ISO C++ forbids initialization in array new
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Maurizio Monge <monge at sns dot it>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 14 May 2004 17:14:14 -0700
- Subject: Re: error: ISO C++ forbids initialization in array new
- References: <200405150148.39204.monge@sns.it>
On Sat, May 15, 2004 at 01:48:39AM +0200, Maurizio Monge wrote:
>
> Hello,
> I apologize if this is not the proper mailing list for my question.
>
> the following code:
>
> int* b = new int[7](3);
>
> Compiles on gcc-3.3.1 (unless -pedantic), but it alwais fails on gcc-3.4.
> I found nothing about it on manuals, has it been intentionally removed or
> is this a bug in gcc-3.4?
It is a bug in your code; this is not valid C++. There is no commitment
that gcc releases will continue to accept invalid code just because
previous gcc releases did. 3.4 has a new C++ parser that is much closer
to the ISO C++ standard than the old one.
For constructs of this sort, where you need to dynamically allocate an
array of objects initialized in the same manner, std::vector is a good
choice, e.g. instead of
int* b = new int[size](value);
try
std::vector<int> b(size, value);