Topic: [QUERY] non-constant array subscript allowed for new?


Author: John Lilley <jlilley@empathy.com>
Date: 1997/01/17
Raw View
Steve Clamage wrote:

> jlilley@empathy.com (John Lilley) writes:
> >I have a question about new-expression in the grammar.

> I believe you are misreading the draft. The new-type-id looks like this:

Yes, all of you are right -- thanks for setting me straight on this one!

john
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jlilley@empathy.com (John Lilley)
Date: 1997/01/12
Raw View
Hi!

I have a question about new-expression in the grammar.  Apparently, the
subscripted form of the new-type-id only accepts a constant expression
for the subscript (according to the may96 DWP).  But several compilers
and lots of code I have seen uses non-constant expressions like:

void f() {
    int length = 7;
    char *p = new char[length];
}

Popular books and standard libraries have examples of this usage.  Is
this version of the draft wrong?  Should non-constant expressions be
allowed?  How else does one allocate a variable-sized array of char?

john lilley


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/01/12
Raw View
John Lilley writes:

> Hi!
> I have a question about new-expression in the grammar.  Apparently, the
> subscripted form of the new-type-id only accepts a constant expression
> for the subscript (according to the may96 DWP).

[snip]

> Is this version of the draft wrong?  Should non-constant expressions
> be allowed?  How else does one allocate a variable-sized array of
> char?

In fact, non-constant expressions are allowed only as the first
subscript, that is:

new int[n][2]; // is valid, but
new int[3][n]; // in not, unless n is a constant

This is specified in [expr.new]:
          direct-new-declarator:
                  [ expression ]
                  direct-new-declarator [ constant-expression ]

7 Every constant-expression in a direct-new-declarator shall be an inte-
  gral constant expression (_expr.const_) and  evaluate  to  a  strictly
  positive  value.  The expression in a direct-new-declarator shall have
  integral type (_basic.fundamental_) with a non-negative value.  [Exam-
  ple:  if  n  is  a variable of type int, then new float[n][5] is well-
  formed (because n is the expression of a  direct-new-declarator),  but
  new float[5][n]   is   ill-formed   (because  n  is  not  a  constant-
  expression).  If n is negative, the effect of new float[n][5] is unde-
  fined.  ]


This is unchanged in Dec'96 DWP.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "John Hickin" <hickin@nortel.ca>
Date: 1997/01/13
Raw View
Are you reading this correctly?  I don't have access to the May version but
the Sept version has the following:

new-expression:
                  ::opt new new-placementopt new-type-id new-initializeropt
                  ::opt new new-placementopt ( type-id ) new-initializeropt
          new-placement:
                  ( expression-list )
          new-type-id:
                  type-specifier-seq new-declaratoropt
          new-declarator:
                  ptr-operator new-declaratoropt
                  direct-new-declarator
          direct-new-declarator:
                  [ expression ]
                  direct-new-declarator [ constant-expression ]
          new-initializer:
                  ( expression-listopt )


Of particular interest is:

 direct-new-declarator:
                  [ expression ]
                  direct-new-declarator [ constant-expression ]

I interpret this to mean that the first dimension may be a general expression
while the other(s) must be constant.  As usual you can't depend on existing
implementations to guide you; g++ accepts a non-constant expression for the
constant-expression part in the above rule.


--
John Hickin      Nortel Technology, Montreal, Quebec
(514) 765-7924   hickin@nortel.ca




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]