Re: Difference between new A and new A()
Am 25.11.2010 23:48, schrieb Martin Bonner:
On Nov 25, 8:41 am, Mathias Gaunard<loufo...@gmail.com> wrote:
On Nov 24, 10:48 am, sourav<souravs...@gmail.com> wrote:
What is the difference between
A *a = new A;
A *a = new A();
While A *a = new A; is supposed to only be a default-initialization,
it seems to be a value-initialization on a variety of popular
compilers.
How can you tell? Given that a default initialized object (of
fundamental type) doesn't have a defined value, there is no way to
tell (that I can see).
In portable mode: You can look at the bit values by reinterpreting
the memory as an unsigned char array. In fact the so-called
object-representation is defined this way, see 3.9/4:
"The object representation of an object of type T is the sequence of N
unsigned char objects taken up by the object of type T, where N equals
sizeof(T). The value representation of an object is the set of bits that
hold the value of type T."
3.10/10 makes accessing the stored value of an object through an lvalue
(or glvalue in the new expression catagories) via an aggregate of char
or unsigned char defined [The current wording uses an unfortunate
ordering in the bullet list that can be read that only single char
access would be valid. This is clearly an oversight and is in the
process of becoming fixed. E.g. the current wording for core issue 1116,
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1116
corrects this].
Further locations in the standard make it valid to look at the bits this
way even before the life-time of an object has started or after it has
finished (around 3.8).
In non-portable mode: Enjoy undefined behaviour and simply look at the
bit pattern that is produced - but don't tell anyone that I recommended
this ;-) [But I think that most of us have done this at some point in
their lives]
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]