http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56146
Bug #: 56146 Summary: Erroneous char initialization only in template function Classification: Unclassified Product: gcc Version: 4.7.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: 2013.bugzilla.gcc.gnu....@ingomueller.net Consider the following piece of code: ------------------- #include <omp.h> #include <stdio.h> #define INIT_BODY \ int mem_size = 1 << 30; \ \ double start = omp_get_wtime(); \ char* data = new char[mem_size]; \ double end = omp_get_wtime(); \ \ printf( "%f s for new char[%i].\n", end - start, mem_size ); \ delete [] data; template<typename T> void init1() { INIT_BODY } void init2() { INIT_BODY } int main(int argc, char* argv[]) { init1<int>(); // with template: takes about a second init2(); // without template: takes a few microseconds } ------------------- I expect the two functions to behave exactly the same way, as they have exactly the same body. Note that the template parameter of init1 is not used. However, in init1, the new char array seems to be initialized. As far as I know, new arrays of PODs should not be initialized by "new". In any way, it is not understandable why the two functions differ in their behavior. Note that I use OpenMP just for its handy timer function. Any other timer (and compiling without -fopenmp) yields the same result. I also measured the number of minor page faults, which correspond exactly to what I expect for initializing the new array. Also note that doubling mem_size doubles both the time and the number of page faults. Last but not least, inverting the order of the two function calls or repeating them does not change anything. Conclusion: I think that arrays constructed by "new" are erroneously initialized when called from a templated function. I observed the erroneous behavior (as described above) in the following compilers: gcc 4.5.3 gcc 4.6.1 gcc 4.6.2 gcc 4.7.1 I observed the correct behavior (both timers yield the same, very small amount of time) in the following compilers: gcc 4.4.3 gcc 4.8-20130120 icc 12.1.2 clang: 3.1 If it matters, I am using Ubuntu 10.04.4.