Topic: Initializers for dynamically allocated arrays


Author: Jim Fischer <jfischer@polymail.cpunix.calpoly.edu>
Date: 1997/05/04
Raw View
I was reading through section 5.3.4 "New [expr.new]" in the Dec. '96 C++ draft today, looking for information on the use of initializers with dynamically allocated arrays.

In section 5.3.3 of the ARM there is an statement which says,

"No initializers can be specified for arrays. Arrays of objects of a class with constructors can be created by operator *new* only if the class has a default constructor (sect. 12.1). In that case, the default constructor will be called for each element i

This is pretty much what I've always known and worked with. However, I was recently told by someone in the comp.lang.c++ news group that you can now use an initializer when dynamically allocating arrays:

  int* a = new int[SIZE](initial_value_for_each_element);

Obviously, I have my doubts that this is correct, which explains why I'm doing some research on the subject.

FWIW, the only information I could find in the new draft regarding the initialization of array elements is in section 8.5.1 /Aggregates/ [dcl.init.aggr]. This is where the use of "brace-enclosed, comma-seperated lists of initializers for the members of th

So, I'm wondering if the the proposed standard (or if the current standard for that matter) defines the use of initializers with dynamically allocated arrays. I wasn't able to determine the answer to this question from the available text in section 5.3.3

TIA for any feedback you might be able to provide on this subject...


Jim Fischer
Computer Engineering, Senior
Cal Poly, SLO
jfischer@polymail.calpoly.edu



Works Cited:

     Ellis, M., Stroustrup, B. "The Annotated C++ Reference Manual". Addison-Weseley: Reading, MA. 1990.

     Merril, Jason. "The ANSI/IOS C++ Draft: December 1996 Working Paper." Chapter 8. Declarators. Cygnus Solutions: Sunnyvale, CA. URL: http://www.cygnus.com/misc/wp/dec96pub/decl.html (3 May 1997).
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/05/04
Raw View

Jim Fischer writes:

> I was reading through section 5.3.4 "New [expr.new]" in the Dec. '96
> C++ draft today, looking for information on the use of initializers
> with dynamically allocated arrays.

>   int* a = new int[SIZE](initial_value_for_each_element);

> Obviously, I have my doubts that this is correct, which explains why I'm doing some research on the subject.

You're right in having doubts, it is really invalid.  A new expression
[expr.new] paragraph 14 of an array may either not have an
new-initializer or have an empty new-initializer ().  Any other
new-initializer for an array is ill-formed.

--
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
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]