Topic: Initialization of const dynamic objects


Author: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
Date: 7 Nov 92 15:57:17 GMT
Raw View
In article <1deqlgINNmc0@darkstar.UCSC.EDU>, daniel@cse.ucsc.edu (Daniel R. Edelson) writes:>
>  new const int;
>
> This allocates an uninitialized const object.
> Seems like this should be explicitly prohibited (assuming
> there's no constructor, of course.) Is it?
> ...
> None of the compilers I tested detected this error.

Borland 3.1 does detect it. In fact I don't think it is necessary for a
compiler to spot such a mistake as the action of `new const int' is perfectly
well defined even if utterly useless!

BTW Borland rejects    `new const A' (an implicit call to A::A()) but not
`new const A()' (an explicit call to A::A())

Does anyone know if this is the _standard_ behaviour?

    \\   ( )  NO BULLSHIT! from BAM (Brian McCauley)
 .  _\\__[oo  ============
.__/  \\ /\@
.  l___\\     E-mail: B.A.McCauley@bham.ac.uk
 # ll  l\\
###LL  LL\\




Author: daniel@cse.ucsc.edu (Daniel R. Edelson)
Date: 6 Nov 1992 22:13:04 GMT
Raw View
Section 7.1.6 contains the following statement:

 ``Unless explicitly declared extern, a const
 object does not have external linkage and
 must be initialized.''

Does that prohibit the following code?

 new const int;

This allocates an uninitialized const object.
Seems like this should be explicitly prohibited (assuming
there's no constructor, of course.) Is it?

Incidently, section 5.3.3 says:

 ``If [a new-initializer is] not [present], the object
 will start out with an unspecified value.''

Thus, an unitialized const dynamic object has and keeps
forever an unspecified value. This should be prohibited.

None of the compilers I tested detected this error.

Daniel Edelson





Author: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Date: Tue, 10 Nov 1992 01:28:03 GMT
Raw View
In article <1deqlgINNmc0@darkstar.UCSC.EDU> daniel@cse.ucsc.edu (Daniel R. Edelson) writes:
>
>Section 7.1.6 contains the following statement:
>
> ``Unless explicitly declared extern, a const
> object does not have external linkage and
> must be initialized.''
>
>Does that prohibit the following code?
>
> new const int;
>
>This allocates an uninitialized const object.

 IMHO there is no such thing as a const *object*. 'const' controls
the access to an object, const T and T are the same type (this not
how the ARM is written though).

 However,

 const int *p=new int;

makes p a pointer with const access to an undefined int. This is just like

 int x; // no init
 const int *p=&x;

>Seems like this should be explicitly prohibited (assuming
>there's no constructor, of course.) Is it?
>
>Incidently, section 5.3.3 says:
>
> ``If [a new-initializer is] not [present], the object
> will start out with an unspecified value.''
>
>Thus, an unitialized const dynamic object has and keeps
>forever an unspecified value. This should be prohibited.

 Not necessarily. With p as above

 *(int*)p=5; // p now points to 5, we can cast away const :-(

 And of course

 x=5;

--
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------