Topic: Defect Report: Should new T and new T() initialise the memory differently for same type T
Author: rmaddox@isicns.com (Randy Maddox)
Date: Fri, 5 Jul 2002 19:40:19 GMT Raw View
ssahoo@versant.com (Sanjeeb Sahoo) wrote in message news:<4a1255b1.0207031341.4d5af3cc@posting.google.com>...
> [ Forwarded to C++ Committeee. -sdc ]
>
> This is in reference to C++ standard issue #302(Value-initialization
> and generation of default constructor), which is a follow up to issue
> 178(More on value-initialization).
> As per the proposal in 302,
> while initialising the memory in evaluating the expression new T(),
> compiler is free to zero out the memory and then call the compiler
> synthesized default constructor for a type T which is "non-POD but no
> user-written constructor" kind. Where for new T, zeroing out is not
> done.
>
> There are couple of issues...
> 1.Imagine a user who initialises the memory inside new operator. In
> that case, because of zeroing out, those data will be lost.
> Initialising memory inside ew operator is typical in Object Database
> vendor code.
I am at a loss to understand why you initialize memory inside your new
operator since that should be returning raw memory that is then
initialized by the constructor. Constructors, by definition, do
initialzation, while new, by definition, returns raw memory to be
initialized.
> 2.For the same type T, is it not bit surprising that new T and new T()
> behaves differently. I thought, value-initialisation was primarily
> added to resolve static object initialisation and aggregate
> initialisation. Should it be extended to initialising object allocated
> from heap??
I would expect new T and new T() to behave identically, i.e., to
allocate raw memory that is then passed to the constructor for
initialization.
Randy.
>
> Thanks,
> Sahoo
>
>
>
> [ 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://www.jamesd.demon.co.uk/csc/faq.html ]
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Andrew Koenig <ark@research.att.com>
Date: Fri, 5 Jul 2002 21:12:18 GMT Raw View
Randy> I would expect new T and new T() to behave identically, i.e., to
Randy> allocate raw memory that is then passed to the constructor for
Randy> initialization.
Unfortunately, there are two reasonable expectations that are
impossible to achieve at the same time
// example 1a
int* p = new int;
int i;
// example 1b
int* p = new int();
int i = int();
It would be nice for *p and i to have the same initialization properties
in each case.
// example 2
int* p = new int;
int* q = new int();
It would be nice for *p and *q to have the same initialization properties.
It should be clear that it is not possible for both example 1 and
example 2 to work ``right'' at the same time.
The revised standard picks examples 1a and 1b over example 2.
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ssahoo@versant.com (Sanjeeb Sahoo)
Date: 5 Jul 2002 06:44:14 GMT Raw View
[ Forwarded to C++ Committeee. -sdc ]
This is in reference to C++ standard issue #302(Value-initialization
and generation of default constructor), which is a follow up to issue
178(More on value-initialization).
As per the proposal in 302,
while initialising the memory in evaluating the expression new T(),
compiler is free to zero out the memory and then call the compiler
synthesized default constructor for a type T which is "non-POD but no
user-written constructor" kind. Where for new T, zeroing out is not
done.
There are couple of issues...
1.Imagine a user who initialises the memory inside new operator. In
that case, because of zeroing out, those data will be lost.
Initialising memory inside ew operator is typical in Object Database
vendor code.
2.For the same type T, is it not bit surprising that new T and new T()
behaves differently. I thought, value-initialisation was primarily
added to resolve static object initialisation and aggregate
initialisation. Should it be extended to initialising object allocated
from heap??
Thanks,
Sahoo
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Andrew Koenig <ark@research.att.com>
Date: Fri, 5 Jul 2002 15:50:24 GMT Raw View
Sanjeeb> 1.Imagine a user who initialises the memory inside new operator. In
Sanjeeb> that case, because of zeroing out, those data will be lost.
The right place to initialize memory is in a constructor.
Sanjeeb> 2.For the same type T, is it not bit surprising that new T and new T()
Sanjeeb> behaves differently. I thought, value-initialisation was primarily
Sanjeeb> added to resolve static object initialisation and aggregate
Sanjeeb> initialisation. Should it be extended to initialising object allocated
Sanjeeb> from heap??
No. It is important to be able to write
struct X { char buffer[4096]; }
X* xp = new X;
without having to go through the overhead of initializing the buffer.
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]