the following function int* f(int n) { return new int[n](); } translates to _Z1fi: .LFB2: pushl %ebp .LCFI0: movl %esp, %ebp .LCFI1: sall $2, 8(%ebp) leave .LCFI2: jmp _Znaj which does not default-initialize the array.
new int[2] () is broken in 3.4.x, see PR 20427.
Testcase which was attached on PR 20427: #include <iostream> void f(int n) { int * a = new int [n] () ; for (int i = 0; i < n; ++i) { std::cout << a[i]++ << ' '; } std::cout << '\n'; delete[] a; } int main() { f(5); f(5); f(5); f(5); } Which shows the issue more clearer.
3.2.3 and 3.0.4 rejected the code: t.cc: In function `void f(int)': t.cc:4: variable-sized object of type `int[n]' may not be initialized No recent (from 2.95.3 and above) GCC version got this right (well at least the versions I tested). Here is a self contained testcase which fails and should not: extern "C" void abort (); void f(int n) { int * a = new int [n] () ; for (int i = 0; i < n; ++i) { if (a[i]) abort (); a[i]++; } delete[] a; } int main() { f(5); f(5); f(5); f(5); } Even ICC 8.1 gets this wrong :).
Fixed for 4.0.3 and above.