Bug 23480 - default-initializing variable size array new expression for basic types does not work
Summary: default-initializing variable size array new expression for basic types does ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: 4.0.3
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2005-08-19 12:45 UTC by Avi Kivity
Modified: 2005-10-27 16:56 UTC (History)
2 users (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Build: i386-redhat-linux
Known to work:
Known to fail: 3.3.3 3.4.0 4.0.0 4.1.0 2.95.3
Last reconfirmed: 2005-08-20 02:37:17


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Avi Kivity 2005-08-19 12:45:12 UTC
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.
Comment 1 Andrew Pinski 2005-08-19 13:04:54 UTC
new int[2] () is broken in 3.4.x, see PR 20427.
Comment 2 Andrew Pinski 2005-08-20 02:30:30 UTC
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.
Comment 3 Andrew Pinski 2005-08-20 02:37:17 UTC
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 :).
Comment 4 Andrew Pinski 2005-10-27 16:56:58 UTC
Fixed for 4.0.3 and above.