Topic: Syntax question
Author: Jeff Rife <jrife-news@nabs.net>
Date: 1999/09/06 Raw View
Leif =?iso-8859-1?Q?L=F6nnblad?= (leif@thep.lu.se) wrote:
> I don't quite understand the difference between the creation of *p01 and
> *p02. In both cases they are created with 'new POD'. That the address of
> the resulting object is assigned to different kinds of pointers
> shouldn't matter, should it?
Yes, it does.
First of all, I did make a mistake, and all the const pointer lines should
look something like:
const POD* p02 = new POD;
This makes the pointers pointers to const objects. Damn those late-night
news sessions.
If that correction clears up your question, then I'm done. If not, read
on...
In the case of p01, it is a pointer to POD object. This object can be
modified without resulting in undefined behavior.
In the case of p02, it is a pointer to a const POD object. This object
*cannot* be modified without resulting in undefined behavior.
The reason it is ill-formed is that the object is not initialized, and there
is no way to change it after the "new" statement.
> Maybe the second example should be
> something like 'new const POD'?
No, this syntax doesn't exist.
--
Jeff Rife | "This? This is ice. This is what happens to
19445 Saint Johnsbury Lane | water when it gets too cold. This? This is
Germantown, MD 20876-1610 | Kent. This is what happens to people when
Home: 301-916-8131 | they get too sexually frustrated."
Work: 301-770-5800 Ext 5335 | -- Chris Knight, "Real Genius"
---
[ 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: Jeff Rife <jrife-news@nabs.net>
Date: 1999/09/06 Raw View
Francis Glassborow (francis@robinton.demon.co.uk) wrote:
> new A does default initialisation. new A() does zero intialisation of
> POD members and was intended to do default initialisation of non-POD
> members (I say was intended because this got lost and is to be corrected
> by a DR)
According to 5.3.4/15 [expr.new], new T() does default initialization and
cross-references 8.5 [dcl.init] for the definition of "default
initialization".
According to 8.5/5, for non-POD this calls the default constructor, and
for POD does zero-initialization.
What got lost, and why do we need a defect report?
--
Jeff Rife | Coach: Would you like a beer, Norm?
19445 Saint Johnsbury Lane |
Germantown, MD 20876-1610 | Norm: I'd like to see something in a
Home: 301-916-8131 | size-54 sudsy.
Work: 301-770-5800 Ext 5335 |
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/07 Raw View
In article <MPG.123a139455bc72469896de@news.nabs.net>, Jeff Rife <jrife-
news@nabs.net> writes
>According to 5.3.4/15 [expr.new], new T() does default initialization and
>cross-references 8.5 [dcl.init] for the definition of "default
>initialization".
>
>According to 8.5/5, for non-POD this calls the default constructor, and
>for POD does zero-initialization.
>
>What got lost, and why do we need a defect report?
The problem is that the compiler generated default ctor at top level
does nothing for data that does not have a user-defined default ctor if
the class is of non-POD type. The result is that the change of a data
member (or data member of a data member etc.) to one that has a user
defined default ctor switches off zero initialisation everywhere.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: Jeff Rife <jrife-news@nabs.net>
Date: 1999/09/01 Raw View
Christopher Eltschka (celtschk@physik.tu-muenchen.de) wrote:
> Ok, I 've looked it up in CD2.
> According to CD2, for const PODs, as well as PODs containing
> const members, only the new A() syntax is legal (a fine point
> I didn't know before, admittedly). Is that what you meant?
I'm not sure if CD2 is different from the final standard, but you
seem to have got the gist right for POD's, so I'll cover the rest of
the options.
The relevant part is from 5.3.4/15 [expr.new]:
"A new expression that creates an object of type T initializes that
object as follows:
If the new initializer is omitted:
If T is a (possibly cv qualified) non POD class type (or array
thereof), the object is default initialized (8.5). If T is a
const qualified type, the underlying class type shall have a
user declared default constructor.
Otherwise, the object created has indeterminate value. If T is a
const qualified type, or a (possibly cv qualified) POD class type
(or array thereof) containing (directly or indirectly) a member of
const qualified type, the program is ill formed;
If the new initializer is of the form (), default initialization shall
be performed (8.5);"
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/02 Raw View
In article <37CD170B.4D449032@physik.tu-muenchen.de>, Christopher
Eltschka <celtschk@physik.tu-muenchen.de> writes
>However, in all cases where "new A" is legal, my sytatement
>above is absolutely correct (unless I've missed something else,
>that is).
new A does default initialisation. new A() does zero intialisation of
POD members and was intended to do default initialisation of non-POD
members (I say was intended because this got lost and is to be corrected
by a DR)
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/09/02 Raw View
Francis Glassborow wrote:
>
> In article <37CD170B.4D449032@physik.tu-muenchen.de>, Christopher
> Eltschka <celtschk@physik.tu-muenchen.de> writes
> >However, in all cases where "new A" is legal, my sytatement
> >above is absolutely correct (unless I've missed something else,
> >that is).
>
> new A does default initialisation. new A() does zero intialisation of
> POD members and was intended to do default initialisation of non-POD
> members (I say was intended because this got lost and is to be corrected
> by a DR)
Unless the terminology has changed between CD2 and the standard,
default-initialisation of PODs _is_ zero initialisation.
>From CD2, 8.5 [dcl.init]/5:
To default-initialize an object of type T means:
--if T is a non-POD class type (_class_), the default constructor for
T is called (and the initialization is ill-formed if T has no acces-
sible default constructor);
--if T is an array type, each element is default-initialized;
--otherwise, the storage for the object is zero-initialized.
Now, for PODs, the first clause obviously doen's apply.
The second clause just moves from arrays to their members
(therefore does not do a change of init method, since POD
arrays have POD members and non-POD arrays have non-POD members).
So the "otherwise" clause applies, which means zero-initialisation.
Also from CD2, 5.3.4 [expr.new]:
14A new-expression that creates an object of type T initializes that
object as follows:
--If the new-initializer is omitted:
--If T is a (possibly cv-qualified) non-POD class type (or array
thereof), the object is default-initialized (_dcl.init_). If T is
a const-qualified type, the underlying class type shall have a
user-declared default constructor.
--Otherwise, the object created has indeterminate value. If T is a
const-qualified type, or a (possibly cv-qualified) POD class type
(or array thereof) containing (directly or indirectly) a member of
const-qualified type, the program is ill-formed;
--If the new-initializer is of the form (), default-initialization
shall be performed (_dcl.init_);
[... cases with expression list snipped ...]
So again, assuming that "new A" is legal, there are only two cases:
- A is POD. Then with "new A" the object has "indeterminate value",
which is just a way of saying it's not initialized, while with
"new A()" it is default-initialized (which for PODs in CD2 means
zero-initialized).
- A is non-POD. Then both "new A" and "new A()" result in
default-initialisation.
Did those definitions change in the final standard?
---
[ 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: Leif =?iso-8859-1?Q?L=F6nnblad?= <leif@thep.lu.se>
Date: 1999/09/02 Raw View
Jeff Rife wrote:
> From the following (assuming that the non-POD structs have something that
> make them that)...
>
> struct POD {};
> struch PODwithConst { const void *a; };
> struct nonPOD {};
> struct nonPODwithCtor { nonPODwithCTor(); };
>
> we get...
>
> 1. POD* p01 = new POD;
>
> The object pointed to by p01 (hereafter referred to using *p01 syntax)
> has indeterminate value.
>
> 2. POD* const p02 = new POD;
> PODwithConst* p03 = new PODwithConst;
>
> In both cases, the program is ill-formed.
I don't quite understand the difference between the creation of *p01 and
*p02. In both cases they are created with 'new POD'. That the address of
the resulting object is assigned to different kinds of pointers
shouldn't matter, should it? Maybe the second example should be
something like 'new const POD'?
Leif
--
Leif L nnblad, Department of Theoretical Physics 2
S lvegatan 14A, S-223 62 Lund, Sweden
phone: +46-46 2227780, fax: +46-46 2229686
e-mail: <Leif.Lonnblad@cern.ch>, <leif@thep.lu.se>
http://www.thep.lu.se/~leif
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/01 Raw View
In article <37CC0F3F.FB61921@physik.tu-muenchen.de>, Christopher
Eltschka <celtschk@physik.tu-muenchen.de> writes
>This generally does the same, except on PODs, where
>"new A()" performs zero-initialisation, while "new A" doesn't
>initialize at all.
I think you might check that a little further, I think you have over-
simplified.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/09/01 Raw View
Francis Glassborow wrote:
>
> In article <37CC0F3F.FB61921@physik.tu-muenchen.de>, Christopher
> Eltschka <celtschk@physik.tu-muenchen.de> writes
> >This generally does the same, except on PODs, where
> >"new A()" performs zero-initialisation, while "new A" doesn't
> >initialize at all.
>
> I think you might check that a little further, I think you have over-
> simplified.
Ok, I 've looked it up in CD2.
According to CD2, for const PODs, as well as PODs containing
const members, only the new A() syntax is legal (a fine point
I didn't know before, admittedly). Is that what you meant?
If so, the term "oversimplification" seems to be exaggerated
to me (although it's good to know that detail).
However, in all cases where "new A" is legal, my sytatement
above is absolutely correct (unless I've missed something else,
that is).
Maybe you could be more concrete in your answers, instead
of letting me (and all other readers) guess what you had
in mind?
[ 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: nasher <nasher@ichips.intel.com>
Date: 1999/08/31 Raw View
I am looking for references on the following syntax but cannot seem to
find any reference mentioning whether the following syntax is legal or
not.
class A {
public:
A::A(); // Default constructor
....
....
};
int
main()
{
A* a = new A(); // Is this legal or defined ?
.....
}
All the books that I have seem to show in passing that to invoke the
default constructor they use:
A* a = new A;
but I could not find out if the above code with the "new A();" is
syntactically correct or legal or not. Also if you have any pointers to
someplace on the web where I can atleast lookup the standard for this
case, it might be helpful as well.
Any comments ?
Thanks in advance,
-Nilesh.
---
[ 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: Kevin Kostrzewa <tkkost@newsguy.com>
Date: 1999/08/31 Raw View
nasher <nasher@ichips.intel.com> writes:
> I am looking for references on the following syntax but cannot seem to
> find any reference mentioning whether the following syntax is legal or
> not.
[snip]
> int
> main()
> {
> A* a = new A(); // Is this legal or defined ?
> .....
> }
Yes, it is legal. In the grammar, a new-expression can contain an
optional new-initializer (the expression to the right of the type that
you are creating). A new-initializer is an optional expression-list
surrounded by parens.
IMHO, writing new expressions like this is bad practice, though,
because you can find yourself accidently using the same idiom when
declaring an instance of A not allocated on the heap, like so:
A a();
which actually means that a is a function that accepts no parameters
and returns an A.
--
kevin kostrzewa
work: kkostrzewa@csisolutions.com
home: tkkost@newsguy.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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/08/31 Raw View
nasher wrote:
>
> I am looking for references on the following syntax but cannot seem to
> find any reference mentioning whether the following syntax is legal or
> not.
>
> class A {
> public:
> A::A(); // Default constructor
> ....
> ....
> };
>
> int
> main()
> {
> A* a = new A(); // Is this legal or defined ?
> .....
> }
It is legal _and_ defined.
>
> All the books that I have seem to show in passing that to invoke the
> default constructor they use:
> A* a = new A;
This generally does the same, except on PODs, where
"new A()" performs zero-initialisation, while "new A" doesn't
initialize at all.
[...]
[ 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 ]