Thread: valgrind and "uninitialized" member values

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    valgrind and "uninitialized" member values

    The meat of this question is: does the C++ standard promise that otherwise uninitialized class members are initialized to zero when the constructor is called? I am under the impression that it does, and certainly gcc behaves that way (if I have a member int, and I don't refer to it in the constructor, it is always zero in the object).

    However, I've noticed that if I subsequently access that member, valgrind calls this an error ("Conditional jump or move depends on uninitialised value(s)"). Does this indicate:

    1) That I'm wrong, all class members must be explicitly initialized or else their value is undefined, and I have been relying on a non-standard compiler feature.
    2) That valgrind is wrong, since the member was initialized to zero.
    3) That the compiler which created the executable is wrong in implementation, because valgrind finds this "uninitialized" value, despite the fact its value is zero, which means it clearly was initialized.

    I presume #2 but don't know enough about binary executables to say that it couldn't be #3, or to say if it is impossible for valgrind not to do this because of something about the nature of exe's and C++. Or something like that
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    1) That I'm wrong, all class members must be explicitly initialized or else their value is undefined, and I have been relying on a non-standard compiler feature.
    All class members must be explicitly initialized.

    Jim

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ...Isn't the default constructor called when they are not ?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The only time the variables may be initialized by default is if the only constructor you have is the default constructor. However you should always initialize your variables to a know value. If you have defined any constructor then the member variables will definitely not be initialized.

    Jim
    Last edited by jimblumberg; 04-20-2011 at 09:07 AM.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If it's a basic data type, and you don't initialize it, then it isn't initialized. No matter what sort of constructors you do or do not write.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    The meat of this question is: does the C++ standard promise that otherwise uninitialized class members are initialized to zero when the constructor is called? I am under the impression that it does, and certainly gcc behaves that way (if I have a member int, and I don't refer to it in the constructor, it is always zero in the object).
    Where mem-initializer-id means the base class or member variable name, the rules are:
    Quote Originally Posted by C++03 Clause 12.6.2 Paragraph 4
    If a given nonstatic data member or base class is not named by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then
    — If the entity is a nonstatic data member of (possibly cv-qualified) class type (or array thereof) or a base class, and the entity class is a non-POD class, the entity is default-initialized (8.5). If the entity is a nonstatic data member of a const-qualified type, the entity class shall have a user-declared default constructor.
    — Otherwise, the entity is not initialized. If the entity is of const-qualified type or reference type, or of a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of a const-qualified type, the program is ill-formed.

    After the call to a constructor for class X has completed, if a member of X is neither specified in the constructor's mem-initializers, nor default-initialized, nor value-initialized, nor given a value during execution of the body of the constructor, the member has indeterminate value.
    Quote Originally Posted by C++03 Clause 8.5 Paragraph 5 (part)
    To default-initialize an object of type T means:
    — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    — if T is an array type, each element is default-initialized;
    — otherwise, the object is zero-initialized.
    Hence, the answer to your question is no. However, jimblumberg's assertion that "all class members must be explicitly initialized" is false due to the default-initialization of data members of non-POD class types.

    Quote Originally Posted by jimblumberg
    The only time the variables may be initialized by default is if the only constructor you have is the default constructor. However you should always initialize your variables to a know value. If you have defined any constructor then the member variables will definitely not be initialized.
    This is false, though the recommendation to "always initialize your variables to a know value" is good.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You are absolutely correct, I didn't even consider non-PODs.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-15-2011, 12:31 PM
  2. Error: "illegal call of non-static member function"
    By Helix in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2003, 10:01 PM
  3. "ambiguous access to class/struct/union member"
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2003, 12:15 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM