Topic: order of new, constructor call, and assign


Author: tony@online.tmx.com.au (Tony Cook)
Date: 1998/02/16
Raw View
Steve Clamage (clamage@Eng.sun.com) wrote:
: In article fsf@shell4.ba.best.com, Matt Armstrong <mattdav@shell4.ba.best.com> writes:
: >Given this code:
: >
: >MyObject* globalVar = 0;
: >
: >void MyFunction()
: >{
: >    globalVar = new MyObject;
: >}
: >
: >Does the standard have anything to say about the value of globalVar
: >within MyObject's constructor?  Is it well defined to be 0, or does
: >the standad allow globalVar to be assigned the return value of new
: >_before_ the constructor returns?

: I don't find anything in the standard that forbids globalVar to be
: assigned to before the constructor completes, nor anything that
: requires it. In particular, the built-in assignment operator does
: not have a sequence point, so there is no requirement that all
: side effects on the right-hand side be complete before assignment
: takes place. In addition, the implementation has considerable
: latitude in exactly how the memory allocation and constructor
: invocations occur.

: On that basis, I'd say you can't make any portable assumptions
: about the value of globalVar inside the MyObject constructor.

Does this mean that if the MyObject constructor throws an exception
the value of globalVar is unspecified?

--
        Tony Cook - tony@online.tmx.com.au
---
[ 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: David R Tribble <david.tribble@central.beasys.com>
Date: 1998/02/06
Raw View
Matt Armstrong <mattdav@shell4.ba.best.com> writes:
> >MyObject* globalVar = 0;
> >
> >void MyFunction()
> >{
> >    globalVar = new MyObject;
> >}
> >
> >Does the standard have anything to say about the value of globalVar
> >within MyObject's constructor?  Is it well defined to be 0, or does
> >the standad allow globalVar to be assigned the return value of new
> >_before_ the constructor returns?

Steve Clamage wrote:
> I don't find anything in the standard that forbids globalVar to be
> assigned to before the constructor completes, nor anything that
> requires it. In particular, the built-in assignment operator does
> not have a sequence point, so there is no requirement that all
> side effects on the right-hand side be complete before assignment
> takes place. In addition, the implementation has considerable
> latitude in exactly how the memory allocation and constructor
> invocations occur.
>
> On that basis, I'd say you can't make any portable assumptions
> about the value of globalVar inside the MyObject constructor.

If it's true that built-in operator=() does not have a sequence
point, then it's possible for an implementation to set the value
of globalVar after new() allocates space for a MyObject but prior
to the call to the constructor.

But if that's the case, then what about the values of var2 and var3
in this example?:

    var3 = var2 = globalVar = new MyObject;

The answer seems to be that their values are set at a point in
time that is implementation-defined (relative to the time that the
constructor is called).

-- David R. Tribble, david.tribble@noSPAM.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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1998/02/06
Raw View
In article fsf@shell4.ba.best.com, Matt Armstrong <mattdav@shell4.ba.best.com> writes:
>Given this code:
>
>MyObject* globalVar = 0;
>
>void MyFunction()
>{
>    globalVar = new MyObject;
>}
>
>Does the standard have anything to say about the value of globalVar
>within MyObject's constructor?  Is it well defined to be 0, or does
>the standad allow globalVar to be assigned the return value of new
>_before_ the constructor returns?

I don't find anything in the standard that forbids globalVar to be
assigned to before the constructor completes, nor anything that
requires it. In particular, the built-in assignment operator does
not have a sequence point, so there is no requirement that all
side effects on the right-hand side be complete before assignment
takes place. In addition, the implementation has considerable
latitude in exactly how the memory allocation and constructor
invocations occur.

On that basis, I'd say you can't make any portable assumptions
about the value of globalVar inside the MyObject constructor.

---
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              ]