This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/54984] New: Array allocated with new in a template class is default initialised
- From: "malcolm.parsons at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 19 Oct 2012 10:42:39 +0000
- Subject: [Bug c++/54984] New: Array allocated with new in a template class is default initialised
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54984
Bug #: 54984
Summary: Array allocated with new in a template class is
default initialised
Classification: Unclassified
Product: gcc
Version: 4.6.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: malcolm.parsons@gmail.com
$ cat a.cpp
#include <stdlib.h>
struct Foo {
Foo(size_t size) : x(new char[size]) {}
char *x;
};
int main(int argc, char* argv[])
{
size_t size = 1000000000;
Foo foo(size);
return(0);
}
$ cat b.cpp
#include <stdlib.h>
template <class T>
struct Foo {
Foo(size_t size) : x(new char[size]) {}
char *x;
};
int main(int argc, char* argv[])
{
size_t size = 1000000000;
Foo<char> foo(size);
return(0);
}
gcc 4.6.3 decides that the array in the templated class needs to be initialised
to 0.
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object
--enable-plugin --enable-objc-gc --enable-targets=all --disable-werror
--with-arch-32=i686 --with-tune=generic --enable-checking=release
--build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
$ g++ -O2 a.cpp -o a
$ g++ -O2 b.cpp -o b
$ time ./a
real 0m0.006s
user 0m0.000s
sys 0m0.004s
$ time ./b
real 0m4.732s
user 0m2.676s
sys 0m1.976s
$ g++ -O2 -S a.cpp
$ g++ -O2 -S b.cpp
$ diff -u a.s b.s
--- a.s 2012-10-19 11:37:35.280151921 +0100
+++ b.s 2012-10-19 11:37:13.803790575 +0100
@@ -1,10 +1,10 @@
- .file "a.cpp"
+ .file "b.cpp"
.section .text.startup,"ax",@progbits
.p2align 4,,15
.globl main
.type main, @function
main:
-.LFB15:
+.LFB13:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
@@ -15,13 +15,21 @@
subl $16, %esp
movl $1000000000, (%esp)
call _Znaj
+ leal 1000000000(%eax), %edx
+ .p2align 4,,7
+ .p2align 3
+.L2:
+ movb $0, (%eax)
+ addl $1, %eax
+ cmpl %edx, %eax
+ jne .L2
xorl %eax, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
-.LFE15:
+.LFE13:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
gcc 4.4.3 did not initialise the array in either class.
$ g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
4.4.3-4ubuntu5.1' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls
--enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc
--disable-werror --with-arch-32=i486 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
$ g++ -O2 a.cpp -o a
$ g++ -O2 b.cpp -o b
$ time ./a
real 0m0.003s
user 0m0.004s
sys 0m0.000s
$ time ./b
real 0m0.003s
user 0m0.000s
sys 0m0.000s
I don't expect the array to be initialised unless requested by adding "()": new
char[size]()