In the example listed below, member "i" of object "obj" allocated at frame 
of "bar" routine is not getting default-initialized to 0 but contains any 
garbage left on the stack. That violates clause 8.5.1/7 of the C++ Standard.

$ cat bar.cpp
#include <cstdio>

struct S {
    const char* s;
    int         i;
};
 
void foo() { 
    // Put some garbage on the stack
    S dummy[2];
    for (int i = 0; i < sizeof(dummy); i++) 
        ((char *)&dummy)[i] = -1; 
}
 
int bar() {
    // Allocate object on the stack
    S obj[2] = {
        {"m0"},
        { }
    };
    // Assume fields those not explicitly initialized
    // are default initialized to 0 [8.5.1/7 and 8.5/5]
    if (obj[0].i == 0){
        return 0;
    } else {
        printf("Failed: obj[0].i == '%d', expecting '0'\n", obj[0].i);
        return 1;
    }
};
 
int main(){
    foo();
    return bar();
}

$ g++ bar.cpp ; ./a.out
Failed: obj[0].i == '-1', expecting '0'

-- 
           Summary: Struct member is not getting default-initialized
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: grigory dot zagorodnev at intel dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-redhat-linux
  GCC host triplet: i686-redhat-linux
GCC target triplet: i686-redhat-linux


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

Reply via email to