[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Is Linux Unix?



Jeronimo Pellegrini <pellegrini@mpcnet.com.br> wrote in message news:<2ox9Q-4n5-5@gated-at.bofh.it>...
> > There's another problem with the above C++ code:  If ny and nz
> > aren't constant, you can't write
> > 
> >     double s[ny][nz];
> > 
> > Instead, either you allocate the array in two stages:
> > 
> >     double** s = new (double*)[ny];
> >     for (j=0; j < ny; ++j) s[j] = new double[nz];
> 
> Actually, the first version works -- and both C and C++ (tested here with
> gcc -- not sure it it became a standard or not)) will dynamically allocate
> memory for you. Try this:
[...]
>         int size1, size2, i, j;
>         cin >> size1;
>         cin >> size2;
>         double vec[size1][size2];

Wow, that's news to me!  The first ANSI C++ standard (1997 or 1998) doesn't
allow it.  "The number of elements of the array, the array bound, must be
a constant expression. . . ." (Stroustrup, _The C++ Programming Language_,
3rd ed., p.89.  Although this book isn't the standard document itself,
I don't expect that there would be such a big difference as this.)
And, certainly, neither size1 or size2 above is a constant expression.
I don't know whether the C++ standard has been revised since.

If this feature is or become standard or de facto standard, that's a step
forward for numerical computations.

Thanks for the info,
Ryo



Reply to: