Topic: new class initializer syntax


Author: comeau@panix.com (Greg Comeau)
Date: 1997/03/03
Raw View
In article <Darron.Shaffer-ya02408000R2302971049500001@news.dallas.net> Darron.Shaffer@beasys.com (Darron Shaffer) writes:
>In article <330F3613.5E73@pratique.fr>, Valentin Bonnard
><bonnardv@pratique.fr> wrote:
>
>>James Kanze wrote:
>>>
>>> Valentin Bonnard <bonnardv@pratique.fr> writes:
>>
>>[... Should variables be default innitialised ? ...]
>>>
>>> |>  Also I work on a Mac. Suppose all ptr get initialised to 0;
>>> |>  what would I gain ?
>>>
>>> Reproducibility.  Your program would do the same thing every time it
>>> ran.

It would?  Can you really say that about every single program?

>What initializing all variables amounts to is:
>
>Advantages:
>
>   Behaviour is reproducable -- no random variable values.  Pointer bugs
>   MAY show up much quicker.  If other coding practices are followed
>   (i.e., not using value zero for any enum) other bugs may be detected
>   sooner.

And what about pointer and non-pointer bugs that can't be caught with this?

>Disadvantages:
>
>   Code will be slower.  How much slower depends on the particular piece of
>   code & how good your compiler is.

This is not the only disadvantage.  This kind of issues was raised
about a month or two ago about whether delete should "0" pointers
(perhaps this is even part of that thread -- I haven't been in this group
for a few weeks).  What's thrown out the window is that it is as bad as
good to do such a thing.  Initializations is no different.
The alledged guarantees these things give you are not as clear cut as
being stated.

For instance, initializations may make some other bugs show up slower.
And indeed it may not be reproduceable in all cases.  And of course,
some things cannot be initialializable (and I'm not just speaking about
something such as a const here).

>In other words, this should probably NOT be enforced by language specification.

Anyway, we agree on this.

>It might, however, be nice to have a compiler switch to generates code for
>default initializations.

For sure.

>  This could be used for testing without changing
>source code.  Some shops could compile production code with this switch.

This is another isuue altogether IMO, and is often done as wrongly as the
things it is supposed to be solving, so I am not convinced that alone
should be the reason for such a switch.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
 Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,3421
---
[ 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: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/02/22
Raw View
James Kanze wrote:
>
> Valentin Bonnard <bonnardv@pratique.fr> writes:

[... Should variables be default innitialised ? ...]
>
> |>  Also I work on a Mac. Suppose all ptr get initialised to 0;
> |>  what would I gain ?
>
> Reproducibility.  Your program would do the same thing every time it
> ran.

Yes and no. The first run trash the interrupt/exceptions vectors;
the next one has a very odd behaviour because of that.

> |>  If I try to write to one I will write into the system globals
> |>  (exceptions vector I think). I don't clearly see why do you
> |>  want this behaviour over the current one.
>
> In other words, you think it preferable to have a situation where e.g.:
> an error appears in normal conditions, but not when you run the program
> under the debugger (because the memory is at a different location, and
> "by chance" contains pointers to areas where it does no harm).

If there was a value which trap for every type, I would be
happy to have my debugger (compiler in debug mode) use it
to innitiase my variables.

Saddly, there isn't such a thing for integers (on processors
I know of); OTOH, it should be possible with floating points
(signaling NAN) and pointer (-1 or an odd number like that =>
bus error).

If you innitialise everything to 0 for release code it means
that the behaviour will be the same but it may not be acceptable
when you allocate 100 MB blocks (ex. Photoshop) and C++ should
allow you to write any app including memory intensives ones.

If you only innitialise to 0 in debug code it means that real
bugs will be hiden at the only time they should show up.

--

Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ 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: Darron.Shaffer@beasys.com (Darron Shaffer)
Date: 1997/02/24
Raw View
In article <330F3613.5E73@pratique.fr>, Valentin Bonnard
<bonnardv@pratique.fr> wrote:

>James Kanze wrote:
>>
>> Valentin Bonnard <bonnardv@pratique.fr> writes:
>
>[... Should variables be default innitialised ? ...]
>>
>> |>  Also I work on a Mac. Suppose all ptr get initialised to 0;
>> |>  what would I gain ?
>>
>> Reproducibility.  Your program would do the same thing every time it
>> ran.
>
>Yes and no. The first run trash the interrupt/exceptions vectors;
>the next one has a very odd behaviour because of that.
>

On a Mac, if you use the "EvenBetterBusError" extension, all writes to a zero
pointer will produce an error.

In addition, the exception vectors are not at physical address zero after
startup.  (various low-memory globals are close to zero, but not AT zero).

What initializing all variables amounts to is:

Advantages:

   Behaviour is reproducable -- no random variable values.  Pointer bugs
   MAY show up much quicker.  If other coding practices are followed
   (i.e., not using value zero for any enum) other bugs may be detected
   sooner.

Disadvantages:

   Code will be slower.  How much slower depends on the particular piece of
   code & how good your compiler is.


In other words, this should probably NOT be enforced by language specification.
It might, however, be nice to have a compiler switch to generates code for
default initializations.  This could be used for testing without changing
source code.  Some shops could compile production code with this switch.
--
Darron Shaffer
Darron.Shaffer@beasys.com
---
[ 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: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/02/15
Raw View
David R Tribble wrote:
>
> > David R Tribble (I) wrote:
> > > All the other object-oriented languages guarantee that every variable
> > > (and every reference) has a well-defined initial value, so why not C++?
> > > How many times have people been bitten by uninitialized data?  When
> > > will the madness stop?
>
> Marcelo Cantos <marcelo@mds.rmit.edu.au> responded:
> > Simple.  Performance.  If I have a 1 million element array that I
> > want to initialise to all ones, it could be very inefficient to
> > first initialise it to all zeros.
>
> This is an argument for an enhancement to the language to allow you to
> specify an initial value for a member (other than zero, which should be the
> default).  This is not a good argument for allowing variables to exist
> with trash in them.

There are very good arguments. WHat if you want different default
values in different elements of the array ?

BTW: new [] with copy construction could be added in C++; why
isn't it already there ?

Also I work on a Mac. Suppose all ptr get initialised to 0;
what would I gain ?

If I try to write to one I will write into the system globals
(exceptions vector I think). I don't clearly see why do you
want this behaviour over the current one.

In Ada 95 the situation is even worst.

--

Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ 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: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/02/16
Raw View
David R Tribble wrote:
>
> > David R Tribble (I) wrote:
> > > All the other object-oriented languages guarantee that every variable (and
> > > every reference) has a well-defined initial value, so why not C++?  How
> > > many times have people been bitten by uninitialized data?  When will the
> > > madness stop?
>
> Marcelo Cantos <marcelo@mds.rmit.edu.au> responded:
> > Simple.  Performance.  If I have a 1 million element array that I
> > want to initialise to all ones, it could be very inefficient to
> > first initialise it to all zeros.
>
> This is an argument for an enhancement to the language to allow you to
> specify an initial value for a member (other than zero, which should be the
> default).  This is not a good argument for allowing variables to exist
> with trash in them.

There are very good arguments. WHat if you want different default
values in different elements of the array ?

BTW: new [] with copy construction could be added in C++; why
isn't it already there ?

Also I work on a Mac. Suppose all ptr get initialised to 0;
what would I gain ?

If I try to write to one I will write into the system globals
(exceptions vector I think). I don't clearly see why do you
want this behaviour over the current one.

In Ada 95 the situation is even worst.

--

Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ 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: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/02/18
Raw View
> David R Tribble (I) wrote:
> > All the other object-oriented languages guarantee that every variable (and
> > every reference) has a well-defined initial value, so why not C++?  How
> > many times have people been bitten by uninitialized data?  When will the
> > madness stop?
>>
>> Marcelo Cantos <marcelo@mds.rmit.edu.au> responded:
>> > Simple.  Performance.  If I have a 1 million element array that I
>> > want to initialise to all ones, it could be very inefficient to
>> > first initialise it to all zeros.
>>
>> This is an argument for an enhancement to the language to allow you to
>> specify an initial value for a member (other than zero, which should be the
>> default).  This is not a good argument for allowing variables to exist
>> with trash in them.

Valentin Bonnard <bonnardv@pratique.fr> replied:

>These are very good arguments. What if you want different default
>values in different elements of the array ?

Perhaps C++ needs a syntax to specify that you do not want the default
initializer value, that you will provide the initializer explicitly in
the constructors.  (Perhaps using the new 'explicit' keyword?)

>BTW: new [] with copy construction could be added in C++; why
>isn't it already there ?

Good point.  It would be analogous to the placement new, except it would
apply to the new[] operator.

>Also I work on a Mac. Suppose all pointers get initialised to 0;
>what would I gain?
>
>If I try to write to one I will write into the system globals area
>(exceptions vectors, I think). I don't clearly see why do you
>want this behaviour over the current one.

If your program writes data through a null pointer, it's exhibiting
undefined behavior, and all bets are off on whether it works benignly or
not; you should check your pointers before dereferencing them, or write
your code so that such a situation is impossible.  Would you prefer
writing to address 0x0000000 in a badly written program or to some random
(trash) address instead?  Either way, it's the fault of the program, not
a faulty choice for the pointer's initial value.

If you don't initialize pointers to NULL that don't yet point to valid
objects in memory, just what value should you initialize them to?  NULL is,
by definition, a pointer value that means 'does not point to any valid
object'; in fact it's the only value for which this is guaranteed.  My
argument has been that pointers (as well as all the other built-in data
types) should have default initializer values, just like all user-defined
class types.  And the correct choice for pointer initializers is NULL.

-- David R. Tribble, david.tribble@central.beasys.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         ]
[ 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: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/02/18
Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:

|>  David R Tribble wrote:
|>  >
|>  > > David R Tribble (I) wrote:
|>  > > > All the other object-oriented languages guarantee that every variable (and
|>  > > > every reference) has a well-defined initial value, so why not C++?  How
|>  > > > many times have people been bitten by uninitialized data?  When will the
|>  > > > madness stop?
|>  >
|>  > Marcelo Cantos <marcelo@mds.rmit.edu.au> responded:
|>  > > Simple.  Performance.  If I have a 1 million element array that I
|>  > > want to initialise to all ones, it could be very inefficient to
|>  > > first initialise it to all zeros.
|>  >
|>  > This is an argument for an enhancement to the language to allow you to
|>  > specify an initial value for a member (other than zero, which should be the
|>  > default).  This is not a good argument for allowing variables to exist
|>  > with trash in them.
|>
|>  There are very good arguments. WHat if you want different default
|>  values in different elements of the array ?
|>
|>  BTW: new [] with copy construction could be added in C++; why
|>  isn't it already there ?

Probably because nobody proposed it.  Or at least not with a suitable
syntax.

|>  Also I work on a Mac. Suppose all ptr get initialised to 0;
|>  what would I gain ?

Reproducibility.  Your program would do the same thing every time it
ran.

|>  If I try to write to one I will write into the system globals
|>  (exceptions vector I think). I don't clearly see why do you
|>  want this behaviour over the current one.

In other words, you think it preferable to have a situation where e.g.:
an error appears in normal conditions, but not when you run the program
under the debugger (because the memory is at a different location, and
"by chance" contains pointers to areas where it does no harm).

--
James Kanze      home:     kanze@gabi-soft.fr        +33 (0)1 39 55 85 62
                 office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
     -- Conseils en informatique industrielle --
---
[ 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: spitzak@cinenet.net (Bill Spitzak)
Date: 1997/02/20
Raw View
What seems to be wanted is an easy way to initialize members rather
than having to put them in the constructor.  Besides making it harder
to forget to initialize a member (a mistake I do all the time) it
would make the constructors easier to read and remove much of the
problems with writing multiple constructors.

Each "initializer" would have exactly the same effect as though
the initilization code was added to the start of every constructor.
If there is no constructor then one is created.

class A
: public B(1,2,3)  // this line adds ": B(1,2,3)" to all constructors

{

    int x(0); // adds x(0) to all constructors

    ClassX z(1,2,3); // add z(1,2,3) to all constructors

    int foo = void; // garbage, even static instance does not need zero

    int bar; // compatable behavior, but can produce warning message

// This one I'm not sure about, although syntax is more user-friendly:
    int y = 0;  // adds y(0), which may be different than y = 0;

};

I would also add the statement "lvalue = void" to the language.  This
does not change the value of lvalue.  Perhaps it is only legal in
constructors, although it might be useful for eliminating warnings
of uninitialized automatic variables without adding useless code.

Then it should be possible to turn on some warning messages from the
compiler if a constructor fails to initialize all of the member
variables.

Bill Spitzak
spitzak@d2.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         ]
[ 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: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/02/11
Raw View
> David R Tribble (I) wrote:
> > All the other object-oriented languages guarantee that every variable (and
> > every reference) has a well-defined initial value, so why not C++?  How
> > many times have people been bitten by uninitialized data?  When will the
> > madness stop?

Marcelo Cantos <marcelo@mds.rmit.edu.au> responded:
> Simple.  Performance.  If I have a 1 million element array that I
> want to initialise to all ones, it could be very inefficient to
> first initialise it to all zeros.

This is an argument for an enhancement to the language to allow you to
specify an initial value for a member (other than zero, which should be the
default).  This is not a good argument for allowing variables to exist
with trash in them.

> Furthermore, pointers within classes are usually assigned a non-
> zero value, so zero assignment is almost always a waste of time.

Usually?  What if they're not?


-- David R. Tribble, david.tribble@central.beasys.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         ]
[ 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                             ]