This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/2524: missing default initialization of POD types in operator new
- To: gcc-gnats at gcc dot gnu dot org
- Subject: c++/2524: missing default initialization of POD types in operator new
- From: gawrilow at math dot tu-berlin dot de
- Date: 10 Apr 2001 11:02:04 -0000
- Reply-To: gawrilow at math dot tu-berlin dot de
>Number: 2524
>Category: c++
>Synopsis: missing default initialization of POD types in operator new
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Tue Apr 10 04:06:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Ewgenij Gawrilow, Technical University Berlin
>Release: gcc version 2.95.3 20010315 (release)
>Organization:
>Environment:
sparc-sun-solaris2.7
gcc compiled and installed from the release source distribution
without any changes
>Description:
gcc does not emit the initialization code for POD type objects
when they are created via the new operator like this:
double *a=new double();
This behavior violates 5.3.4 [New] par. 15, which states
> if the new-initializer is of the form (), default-initialization
> shall be performed.
According to 8.5 [Initializers] par. 5, default-initialization
of a POD type object means its zero-initialization.
The program attached below checks three possible kinds of new:
scalar, array, and placement. All of them suffer from this
bug - the objects being created have non-zero (garbage) values.
>How-To-Repeat:
Compile and run the attached code. If the compiler is standard-compliant,
it should print two lines of zeroes.
>Fix:
Indeed, it is easy to replace double() thru double(0).
However, in a template code which is expected to be instantiated
with both POD and user-defined types, this work-around is not
feasible, since not every default-constructible type has
an (int) constructor too.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="init.cc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="init.cc"
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bmV3Lmg+Cgp0ZW1wbGF0ZSA8Y2xhc3MgVD4K
dm9pZCB0ZXN0X2luaXQoKSB7CiAgIFQgbG9jYWw9VCgpOwogICBUICpzY2FsYXJfb25faGVhcD1u
ZXcgVCgpOwogICBUICphcnJheV9vbl9oZWFwPW5ldyBUWzFdKCk7CiAgIGRvdWJsZSBhcmVhWyhz
aXplb2YoVCkrc2l6ZW9mKGRvdWJsZSktMSkvc2l6ZW9mKGRvdWJsZSldPXsgMzMzMyB9OwogICBU
ICpwbGFjZW1lbnQ9bmV3KGFyZWEpIFQoKTsKICAgc3RhdGljIGNvbnN0IFQgc3RhdGljX2NvbnN0
PVQoKTsKICAgY291dCA8PCBsb2NhbCA8PCAiICIgPDwgKnNjYWxhcl9vbl9oZWFwIDw8ICIgIiA8
PCBhcnJheV9vbl9oZWFwWzBdIDw8ICIgIgoJPDwgcGxhY2VtZW50WzBdIDw8ICIgIiA8PCBzdGF0
aWNfY29uc3QgPDwgZW5kbDsKfQoKY2xhc3MgRCB7CiAgIGRvdWJsZSBkOwpwdWJsaWM6CiAgIEQo
KSA6IGQoKSB7IH0KICAgRChkb3VibGUgeCkgOiBkKHgpIHsgfQoKICAgZnJpZW5kIG9zdHJlYW0m
IG9wZXJhdG9yPDwgKG9zdHJlYW0mIG9zLCBjb25zdCBEJiBjKSB7IHJldHVybiBvcyA8PCBjLmQ7
IH0KfTsKCmludCBtYWluKCkgewogICBjb3V0IDw8ICJQT0Q6ICAgIjsgdGVzdF9pbml0PGRvdWJs
ZT4oKTsKICAgY291dCA8PCAiY2xhc3M6ICI7IHRlc3RfaW5pdDxEPigpOwogICByZXR1cm4gMDsK
fQo=