Thread: Initialization of primitve types

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Initialization of primitve types

    What are primitive (member) types initialized as in C++?

    My integral members seem consistently to initialize to zero, float/double members often initialize the same, but can occasionally end-up with garbage, causing many headaches.

    Some sources mention primitives are initialized to 0, yet elsewhere I've heard that the entire class is first memset to 0 (definitely not the same thing...)

    So can anyone tell me the definitive list of initialization values for each primitive type?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They are initialised to whatever you initialise them

    Hence, initialise them in the initialisation list to avoid having them contain garbage. If you wanted to, you could value-initialise them, which means zero-initialisation, e.g.,
    Code:
    class X
    {
    public:
        X() : a_(), b_() {}
    private:
        int a_;
        double b_;
    };
    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

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Primitive members are not initialized unless you initialize them. Some compilers might initialize them to 0 in some cases, but you shouldn't rely on that.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    No primitive types are automatically initialised. They're all just random junk, and as it happens random junk frequently contains many zeros.
    If you need a variable to start with a particular value, you are responsible for initialising it, preferably in the declaration.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I see, thanks.
    @laserlight, I thought the default constructor called all default constructors recursively on all of its members, but it seems as if your example is implying that it is only called for non-primitives (leaving the programmer to call these explicitly)?
    Is this the case? It seems a bit counter-intuitive that a Foo member will be initialized with Foo(), but a int/double/primitive member won't be initialized at all...

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by @nthony
    I thought the default constructor called all default constructors recursively on all of its members, but it seems as if your example is implying that it is only called for non-primitive classes (leaving the programmer to call these explicitly)?
    If the class is not a POD type, then its constructors invoke the default constructors of all members of class types that are not listed in their initialisation lists. If not, or if the members are not of class types, then the members are not initialised if they are not listed in the respective initialisation lists.
    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 Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Interesting... I wonder why they chose to do this?
    If I have a Foo member "bar" and am guaranteed that bar() will be called automatically if not explicitly initialized, shouldn't one naturally expect the same behavior for integer member "i" (that i() be called)?. Almost builds a case for using wrapped primitives....

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It seems a bit counter-intuitive that a Foo member will be initialized with Foo(), but a int/double/primitive member won't be initialized at all <<

    I believe it is either done for performance reasons or to remain backwards compatible with C (or both). Primitives and POD types that use only primitives (or other POD types) are all that you have in C, and they're not initialized when you declare them. You get the same behavior in C++.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by @nthony View Post
    My integral members seem consistently to initialize to zero, float/double members often initialize the same, but can occasionally end-up with garbage, causing many headaches.
    A good compiler warns when you use a non-initialized variable.
    Otherwise, just always initialize them to a safe value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM