Topic: char *p = new char[0]?


Author: "Erp" <epulsif@teamdev.com>
Date: 2000/02/17
Raw View
I haven't read all the replies here, but the expression

int * pInt = new int[0];

is the same as

int * pInt = new int;

At least that's what the standard says.


---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/02/17
Raw View

Erp wrote:
>
> I haven't read all the replies here, but the expression
>
> int * pInt = new int[0];
>
> is the same as
>
> int * pInt = new int;
>
Say what?  It says that about
 new int[0]

  When the value of the expression in a direct-new-declarator is zero, the
  allocation function is called to allocate an array with no elements.
  The pointer returned by the new-expression is non-null.

where it says aobut
 new int

   The new-expression attempts to create an object of the type-id

How can a pointer to the an allocation of array with no elements be the same as creation
of a single element?

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/02/17
Raw View
Erp wrote:
>
> I haven't read all the replies here, but the expression
>
> int * pInt = new int[0];
>
> is the same as
>
> int * pInt = new int;
>
> At least that's what the standard says.

It says no such thing.  The types of the expressions are the
same, but not the semantics.  In particular, you can dereference
the result of new int, but not the result of new int[0]. Refer
to 5.3.4 paragraph 7.

--
Steve Clamage, stephen.clamage@sun.com

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Paul Black <paul.black@oxsemi.com>
Date: 2000/02/18
Raw View
"Erp" <epulsif@teamdev.com> wrote:
>
> I haven't read all the replies here, but the expression
>
> int * pInt = new int[0];
>
> is the same as
>
> int * pInt = new int;
>
> At least that's what the standard says.

It doesn't. In the first case *pInt is not valid, in the second case it
is.

Paul

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Erp" <epulsif@teamdev.com>
Date: 2000/02/18
Raw View
You are correct, I misread the online standard, and in section 5.3.3 in the
ARM it's a little ambiguous.
Sorry for the confusion.


"Ron Natalie" <ron@sensor.com> wrote in message
news:38AAE9EC.EFF7960B@sensor.com...
>
>
> Erp wrote:
> >
> > I haven't read all the replies here, but the expression
> >
> > int * pInt = new int[0];
> >
> > is the same as
> >
> > int * pInt = new int;
> >
> Say what?  It says that about
> new int[0]
>
>   When the value of the expression in a direct-new-declarator is zero, the
>   allocation function is called to allocate an array with no elements.
>   The pointer returned by the new-expression is non-null.
>
> where it says aobut
> new int
>
>    The new-expression attempts to create an object of the type-id
>
> How can a pointer to the an allocation of array with no elements be the
same as creation
> of a single element?
>
> ---
> [ 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    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Xiaofeng_Jin" <Xiaofeng_Jin@ims.msn.com>
Date: 2000/01/28
Raw View
What should happen to p, like this? Null or something? Appreciate for help!



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/01/29
Raw View
Xiaofeng_Jin wrote:
>
> What should happen to p, like this? Null or something? Appreciate for help!

The expression
 new char[0] // error
is not valid, and the compiler must issue a diagnostic message.
A constant-expression designating the number of elements must be
greater than zero.

If you use a general expression for the array size, it is allowed
to have the value zero.

void f(int i)
{
 char* p = new char[i]; // ok if i is zero
 ...
}

In this case, you get a non-null pointer to an array with zero elements.
That is, the pointer itself is valid, but you cannot dereference it.
(If i is negative, the results are undefined.)

The special case for other than constant-expressions allows you to
write general array processing code without having to check for zero
size:
 char* p = new char[i];
 for( int j = 0 ; j < i; ++j {
  ... use p[j] ...
 }
 for( char* q = p; q < p+i; ++q ) {
  ... use *q ...
 }
Notice that if i is zero, the loop bodies are never executed.

--
Steve Clamage, stephen.clamage@sun.com

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Dag Henriksson <dag.henriksson@quidsoft.se>
Date: 2000/01/29
Raw View

Steve Clamage wrote:

> Xiaofeng_Jin wrote:
> >
> > What should happen to p, like this? Null or something? Appreciate for help!
>
> The expression
>         new char[0] // error
> is not valid, and the compiler must issue a diagnostic message.
> A constant-expression designating the number of elements must be
> greater than zero.
>

I read it differently

The grammar is:

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

What is not allowed to be zero here is the constant expression.
That is
new char[5][0]; // error
is not valid, but
new char[0];
is quite alright.

-- Dag Henriksson



---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/01/30
Raw View
On Sat, 29 Jan 2000 09:21:10 CST, Dag Henriksson
<dag.henriksson@quidsoft.se> wrote:

>Steve Clamage wrote:
>
>> The expression
>>         new char[0] // error
>> is not valid, and the compiler must issue a diagnostic message.
>> A constant-expression designating the number of elements must be
>> greater than zero.
>>
>
>I read it differently
>
>The grammar is:
>
>direct-new-declarator:
>  [ expression ]
>  direct-new-declarator [ constant-expression ]
>
>What is not allowed to be zero here is the constant expression.
>That is
>new char[5][0]; // error
>is not valid, but
>new char[0];
>is quite alright.

Quoting from 5.3.4 paragraph 6:
"Every constant_$B%e_(Bexpression in a direct_$B%e_(Bnew_$B%e_(Bdeclarator shall be an
integral constant expression (5.19) and evaluate to a strictly
positive value."

I don't see how you can intrepret that to allow
 new char[0];

---
Steve Clamage, stephen.clamage@sun.com

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Dag Henriksson <dag.henriksson@quidsoft.se>
Date: 2000/01/30
Raw View


Steve Clamage wrote:

> On Sat, 29 Jan 2000 09:21:10 CST, Dag Henriksson
> <dag.henriksson@quidsoft.se> wrote:
>
> >Steve Clamage wrote:
> >
> >> The expression
> >>         new char[0] // error
> >> is not valid, and the compiler must issue a diagnostic message.
> >> A constant-expression designating the number of elements must be
> >> greater than zero.
> >>
> >
> >I read it differently
> >
> >The grammar is:
> >
> >direct-new-declarator:
> >  [ expression ]
> >  direct-new-declarator [ constant-expression ]
> >
> >What is not allowed to be zero here is the constant expression.
> >That is
> >new char[5][0]; // error
> >is not valid, but
> >new char[0];
> >is quite alright.
>
> Quoting from 5.3.4 paragraph 6:
> "Every constant-expression in a direct-new-declarator shall be an
> integral constant expression (5.19) and evaluate to a strictly
> positive value."
>
> I don't see how you can intrepret that to allow
>         new char[0];

There is no constant-expression in
new char[0];
If you look at the grammar, you will se that the 0 here is an expression, not a
constant-expression, so what you quote doesn't apply here.
The fact that 0 in other contexts can be intepreted as a constant-expression is
irrelevant.

Look instead at the next paragraph:
5.3.4p7
"When the value of the expression in a direct-new-declarator is zero, the allocation
function is called to allocate an array with no elements."

What you quote does, is to require the constant-expression[s] in

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

to be greater than zero. That is
new char[e][c1][c2][c3][cn];

c1, c2, c3, cn > 0

-- Dag Henriksson



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Michiel Salters <salters@lucent.com>
Date: 2000/01/31
Raw View
Dag Henriksson wrote:
>
> Steve Clamage wrote:

[ on new char [0]

> I read it differently

> The grammar is:

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

> What is not allowed to be zero here is the constant expression.
> That is
> new char[5][0]; // error
> is not valid, but
> new char[0];
> is quite alright.

> -- Dag Henriksson

Aren'y you confusing BNF grammar with C++ grammar?
The '[' above starts a BNF element, IIRC an optional
token. It is not the '[' that is part of an array
declaration in C++.

Michiel Salters


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Mark Williams <markw65@my-deja.com>
Date: 2000/01/31
Raw View


Steve Clamage wrote:
> >
> >The grammar is:
> >
> >direct-new-declarator:
> >  [ expression ]
> >  direct-new-declarator [ constant-expression ]
> >
> >What is not allowed to be zero here is the constant expression.
> >That is
> >new char[5][0]; // error
> >is not valid, but
> >new char[0];
> >is quite alright.
>
> Quoting from 5.3.4 paragraph 6:
> "Every constant_$B%e_(Bexpression in a direct_$B%e_(Bnew_$B%e_(Bdeclarator shall be an
> integral constant expression (5.19) and evaluate to a strictly
> positive value."
>
> I don't see how you can intrepret that to allow
>         new char[0];
>

Because the grammar does not allow a direct-new-declarator to be just
"[constant-expression]", it only allows it to be "[expression]". The
sentence following the one you quoted reads: "*THE* [my emphasis]
expression in a direct-new-declarator shall have integral type (3.9.1)
with a non-negative value." ie _every_ direct-new-declarator contains an
expression, and may also contain arbitrarily many constant-expressions
(conveniently matching up with the grammar). The first sentence only
applies to the constant-expressions, the second to the expression.

Mark Williams


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Dag Henriksson <dag.henriksson@quidsoft.se>
Date: 2000/01/31
Raw View


Steve Clamage wrote:

> On 30 Jan 2000 23:39:21 GMT, Dag Henriksson
> <dag.henriksson@quidsoft.se> wrote:
>
> >Steve Clamage wrote:
> >>
> >> Quoting from 5.3.4 paragraph 6:
> >> "Every constant-expression in a direct-new-declarator shall be an
> >> integral constant expression (5.19) and evaluate to a strictly
> >> positive value."
> >>
> >> I don't see how you can intrepret that to allow
> >>         new char[0];
> >
> >There is no constant-expression in
> >new char[0];
> >If you look at the grammar, you will se that the 0 here is an expression, not a
> >constant-expression,
>
> Yes, but you must also look at the definition of constant-expression,
> in 5.19 paragraph 1. An expression involving only a literal integer
> (among other possibilities) is a constant-expression. Looking at
> 2.13.1, we see that a literal 0 is an integer. Hence 0 is an integral
> constant-expression.

Yes, 5.19 say that 0 is a valid constant-expression. It does NOT say that 0 is
ALWAYS translated to a constant-expression .

Similary, 4.10p1 say that 0 is a valid null pointer constant. Are you saying that 0
is ALWAYS translated to a null pointer constant? If not, what is the different in
that reasoning from yours?

if 0 is translated to a constant-expression in
new char[0];
Why isn't it a syntax error? The grammar say that we need an expression here.

-- Dag Henriksson



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/01/31
Raw View


Michiel Salters wrote:

>
> Aren'y you confusing BNF grammar with C++ grammar?
> The '[' above starts a BNF element, IIRC an optional
> token. It is not the '[' that is part of an array
> declaration in C++.

Nope, the [ is a literal '[' token.  The C++ grammar
rules use a subscript "opt" to indicate optional elements.



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW <allan_w@my-deja.com>
Date: 2000/01/31
Raw View
In article <p9aa9skgc48rilp2nnesg7hc7j1ce5jfrr@4ax.com>,
  Steve Clamage <stephen.clamage@sun.com> wrote:
> On 30 Jan 2000 23:39:21 GMT, Dag Henriksson
> <dag.henriksson@quidsoft.se> wrote:
>
> >Steve Clamage wrote:
> >>
> >> Quoting from 5.3.4 paragraph 6:
> >> "Every constant-expression in a direct-new-declarator shall be an
> >> integral constant expression (5.19) and evaluate to a strictly
> >> positive value."
> >>
> >> I don't see how you can intrepret that to allow
> >>         new char[0];
> >
> >There is no constant-expression in
> >new char[0];
> >If you look at the grammar, you will se that the 0 here is an
> >expression, not a
> >constant-expression,
>
> Yes, but you must also look at the definition of constant-expression,
> in 5.19 paragraph 1. An expression involving only a literal integer
> (among other possibilities) is a constant-expression. Looking at
> 2.13.1, we see that a literal 0 is an integer. Hence 0 is an integral
> constant-expression.

The grammer says that any integer literal is a type of constant-
expression, and any constant-expression is a type of expression.
But 5.3.4/6 refers to "any constant-expression in a
direct-new-declarator". There are two ways to interpret this.

   "...any constant-expression used anywhere within the
   direct-new-declarator". But this would make
      new int[i+0]
   illegal because the expression includes a constant-expression
   which is not strictly non-negative.

   "...any constant-expression, except for constant-expressions
   that happen to be part of the first expression (which is not
   required to be a constant-expression)". But this would make
      new int[0]
   legal and equivalent to
      new int[x] /* where x==0 */

The only reason I assume that the second version is what was
intended, is that (IMHO) the first represents a horrendous rule
which serves no useful purpose. Most of the rules in C++ follow
a fairly consistent logic. This can be misleading, but it's
usually fairly accurate.

After all, if
   { int x=0; return new int[x]; }
is valid, then
   return new int[0];
ought to be valid as well. I would support having a compiler issue
a warning for "new int[0]". (The only reasons I know of to allow
0-length arrays are to reduce logic paths when using general-purpose
algorithms on arrays of unknown size. In "new int[0]" we already
know the size.) But after the warning, the compiler ought to be
required to compile it and produce the obvious results.

--
Allan_W@my-deja.com is a "Spam Magnet," never read.
Please reply in newsgroups only, sorry.


Sent via Deja.com http://www.deja.com/
Before you buy.


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/02/01
Raw View
On 30 Jan 2000 23:39:21 GMT, Dag Henriksson
<dag.henriksson@quidsoft.se> wrote:

>Steve Clamage wrote:
>>
>> Quoting from 5.3.4 paragraph 6:
>> "Every constant-expression in a direct-new-declarator shall be an
>> integral constant expression (5.19) and evaluate to a strictly
>> positive value."
>>
>> I don't see how you can intrepret that to allow
>>         new char[0];
>
>There is no constant-expression in
>new char[0];
>If you look at the grammar, you will se that the 0 here is an expression, not a
>constant-expression,

Yes, but you must also look at the definition of constant-expression,
in 5.19 paragraph 1. An expression involving only a literal integer
(among other possibilities) is a constant-expression. Looking at
2.13.1, we see that a literal 0 is an integer. Hence 0 is an integral
constant-expression.

---
Steve Clamage, stephen.clamage@sun.com

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@Sun.COM>
Date: 2000/02/03
Raw View
Steve Clamage wrote:
>
> The expression
>         new char[0] // error
> is not valid, and the compiler must issue a diagnostic message.
> A constant-expression designating the number of elements must be
> greater than zero.
>
> If you use a general expression for the array size, it is allowed
> to have the value zero.

I was wrong about the first part, misled by wording in the standard
that could possibly be clearer.

The "[0]" in the expression is syntactically a direct-new-declarator.
Section 5.3.4 paragraph 6 says "every constant-expression in a direct-
new-declarator" must be strictly positive, but a general expression
can have a zero value. Since a literal 0 is a constant-expression,
I took that to mean it wasn't allowed, because of the word "every".

But the syntax productions in that section show the following:

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

The sentence in paragraph 6 should apply to the components of
a direct-new-declarator that must be constant-expressions, not
to others that just happen to be constant-expressions. The second
and subsequent dimensions become part of the type of the returned
pointer, so they must be compile-time constants greater than zero.

Viewed that way, the results are more consistent: any dynamically-
allocated array can have zero elements.

--
Steve Clamage, stephen.clamage@sun.com

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]