[Bug c++/52156] New: add warning for allocated array being converted to pointer to base

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Feb 7 19:37:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52156

             Bug #: 52156
           Summary: add warning for allocated array being converted to
                    pointer to base
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: redi@gcc.gnu.org


struct B { int i; B() : i(0) { } };
struct D : B { int j; D() : j(1) { }};

int main()
{
    B* b = new D[2];  // warning
    return b[1].i;
}

It might be a good idea to warn about this code, the allocated array cannot be
safely indexed or deleted through any type except D*

There should be a warning if a new-expression allocates an array of D and the
result undergoes pointer conversion to a type that is not cv D*



More information about the Gcc-bugs mailing list