Warnung vor Uninitialized member variables in gcc?



  • Hallo,
    ich verwende gcc mit -Wall und -Wextra, aber leider lässt er mir folgenden Code durchgehen. Kann ich ihn irgendwie dazu bringen mich davor zu warnen? bla ist nicht initialisiert, und kann es auch garnicht werden. Gerade bei großen Klassen bringt mich das häufig in Probleme 😞

    #include <iostream>
    
    class cl
    {
        public:
            cl();
            void print();
        private:
            int bla;
    };
    
    cl::cl()
    {
    }
    
    void cl::print()
    {
        std::cout << "Bla: " <<  bla << std::endl;   
    }
    
    int main()
    {
        cl t;
        t.print();
        return 1;
    }
    

    Grüße,
    Thomas



  • Warum soll das nicht möglich sein?

    class cl
    {
        public:
            cl();
            void print();
        private:
            int bla;
    };
    
    cl::cl() : bla(0) // oder einen anderen Wert, anstatt Null.
    {
    }
    

    Der Compiler brint meistens schon sinnvolle Warnungen. 😉



  • elfstone schrieb:

    bla ist nicht initialisiert, und kann es auch garnicht werden.

    Bla WIRD aber initialisiert, egal was du machst. Wenn du garnichts angibst, wirds vom Compiler implizit default-initialisiert.



  • elfstone schrieb:

    ...

    Ich bin da auch schon drüber gestolpert. Wenn man
    g++ -Wall -Wextra -O3 source.cpp aufruft, geht es ulkigerweise. Für -O{0,1,2} geht es nicht. Nur für -O3 .
    Ich habe keinen Schimmer, woran das liegt. Ich schätze mal, dass es ein Bug ist, denn eine uninitialisiert benutzte Variable sollte unabhängig vom Optimierungsgrad eine Warnung produzieren.
    Die gcc-Version ist 4.2.4



  • pumuckl schrieb:

    elfstone schrieb:

    bla ist nicht initialisiert, und kann es auch garnicht werden.

    Bla WIRD aber initialisiert, egal was du machst. Wenn du garnichts angibst, wirds vom Compiler implizit default-initialisiert.

    Das stimmt leider nicht, denn:

    ISO/IEC 14882:2003 8.5 §9 schrieb:

    If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or
    array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying
    class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a non-
    static object, the object and its subobjects, if any, have an indeterminate initial value
    90)
    ; if the object or any
    of its subobjects are of const-qualified type, the program is ill-formed

    Der Wert für nicht initialisierte POD-Typen darf also beliebig sein, wenn er nicht explizit initialisiert wird.



  • Tatsächlich, mit -O3 kriege ich die Warnung auch.. das hilft zumindest schonmal, danke.



  • Mit Warnoption -Weffc++ bringt g++ folgende Meldung:

    main.cpp: In constructor ‘cl::cl()’:
    main.cpp:12: warning: ‘cl::bla’ should be initialized in the member initialization list
    

Anmelden zum Antworten