Topic: >>new char[0] and new char[1]... difference?


Author: "Eugene Radchenko" <eugene@qsar.chem.msu.su>
Date: 1995/06/28
Raw View
<clamage@Eng.Sun.COM> (Steve Clamage) writes in  comp.lang.c++

>Subject: [NEWS] Re: new char[0] and new char[1]... difference?
>Date: 11 Jun 1995 20:47:04 GMT
>
>In article 18210@cato.robots.ox.ac.uk, mrb@robots.ox.ac.uk (Mike Robert Brewer) writes:
>>The ARM says on p59 that a zero argument to new returns a pointer to
>>the object:
>>e.g. char *p1 = new char[0];
>
>But "char[0]" is not a valid type, and the expression "new char[0]"
>is not a valid expression. The ARM says so explicitly in 8.2.4, page 136.
>The C++ draft standard says the same thing.

Certainly char[0] as such is not usable. But what about the common
technique involving constructs like this:
  class Array {
    int arsize;
    char *p;
   public:
    Array(int size)
      : arsize(size), p(new char[size]) {}
    ~Array()  { delete p; }
    int GetSize() { return arsize; }
    void Resize(int newsize); //allocate new block, copy contents, switch
                              //pointers, delete old block
  };

Here Array(0) might be necessary (for instance, if Array object is used to
store dynamically generated objects number of which is not known in advance).
Generally speaking, what type is `char [size]` ?

          Best regards                            Eugene





--
--------------------------------------------------------------------
Eugene V. Radchenko           Graduate Student in Computer Chemistry
E-mail: eugene@qsar.chem.msu.su                Fax: +7-(095)939-0290
Ordinary mail:  Chair of Organic Chemistry, Department of Chemistry,
                      Moscow State University, 119899 Moscow, Russia
*****************  Disappearances are deceptive  *******************