Default initialization of objects??

Discussion in 'C++' started by CodeTrooper, Nov 22, 2011.

  1. CodeTrooper

    CodeTrooper Guest

    I am new to C++ and was trying to find if objects in C++ are
    intialized by default. I wrote a sample program as follows:

    #include <iostream>

    int main()
    {
    class A
    {
    int a, b, c;
    public:
    int getVal() { return c; }
    };

    A obj;
    std::cout << obj.getVal() << std::endl;
    return (0);
    }

    This program prints a value of 0 when compiled with g++ and run on a
    linux system. However when run in Visual Studio's C++, it crashes (but
    it does not crash if I initialize obj before trying to read a member's
    value.

    I tried googling about the behaviour but could not find anything
    definitive. I would like to know if the use of an uninitialized object
    like this results in an undefined behaviour and if there is any
    initialization (maybe, to zeroes) provided by default for the objects.
    From the results I have, it does seem to be the former case, but would
    like to double check as to what the language standard says.
     
    CodeTrooper, Nov 22, 2011
    #1
    1. Advertising

  2. CodeTrooper

    Goran Guest

    On Nov 22, 1:03 pm, CodeTrooper <> wrote:
    > I am new to C++ and was trying to find if objects in C++ are
    > intialized by default.


    In C++, you have primitive types (bool, numbers, pointers, character
    types, enumerations) who are not initialized at all when declared. All
    sorts of aggregation consisting solely of such types (arrays,
    structures, unions) are not initialized either.

    You can initialize them to their default values (e.g. 0 for numbers)
    by doing e.g. int i = int();

    When you create your own type (a class), you can add it a constructor
    and initialize what you want in it. E.g.

    class MyType
    {
    int a;
    public
    MyType() : a() {} // "a" is default-initialized
    };

    If you leave out a() (and if you don't assign it in constructor body),
    then, since a is a primitive type, it's not going to be initialized at
    all.


    > I wrote a sample program as follows:
    >
    > #include <iostream>
    >
    > int main()
    > {
    >     class A
    >     {
    >         int a, b, c;
    >         public:
    >         int getVal() { return c; }
    >     };
    >
    >     A obj;
    >     std::cout << obj.getVal() << std::endl;
    >     return (0);
    >
    > }
    >
    > This program prints a value of 0 when compiled with g++ and run on a
    > linux system. However when run in Visual Studio's C++, it crashes (but
    > it does not crash if I initialize obj before trying to read a member's
    > value.


    I am surprised that it crashes (I would expect it to print garbage),
    but, you tried to read uninitialized variable, which, I believe, is
    undefined behavior.

    > I tried googling about the behaviour but could not find anything
    > definitive. I would like to know if the use of an uninitialized object
    > like this results in an undefined behaviour and if there is any
    > initialization (maybe, to zeroes) provided by default for the objects.
    > From the results I have, it does seem to be the former case, but would
    > like to double check as to what the language standard says.


    There's no default 0-init in C++.

    Goran.
     
    Goran, Nov 22, 2011
    #2
    1. Advertising

  3. Hi CodeTrooper

    CodeTrooper wrote:

    > I am new to C++ and was trying to find if objects in C++ are
    > intialized by default.


    No they are not initialized You have to use an initializer list and You
    should move the class out of main. Most of the time a class gets its own
    unit a cpp and a h file, something like this:

    in filename.h file:

    #ifndef FilenameH
    #define FilenameH

    class A
    {
    int a, b, c;
    public:
    int getVal() { return c; }
    A();
    };

    #endif

    in finename.cpp file

    #include "finename.h"
    A::A()
    : a(0)
    , b(0)
    , c(0)
    {
    }

    include <iostream>
    #include "finename.h"

    int main()
    {
    A obj;
    std::cout << obj.getVal() << std::endl;
    return (0);
    }

    >
    > This program prints a value of 0 when compiled with g++ and run on a
    > linux system. However when run in Visual Studio's C++, it crashes (but
    > it does not crash if I initialize obj before trying to read a member's
    > value.
    >
    > I tried googling about the behaviour but could not find anything
    > definitive. I would like to know if the use of an uninitialized object
    > like this results in an undefined behaviour


    Yes as much as the initial value of the objects internal variables are
    important to the objects behaviour.

    In other words the fact that You are not initializing a, b and c have
    nothing to do with Your program crashing. You should just see a undefined
    number.
    I don't use Visual C++ so I cant test.

    Best regards
    Asger-P
     
    Asger Joergensen, Nov 22, 2011
    #3
  4. Goran <> wrote:
    > I am surprised that it crashes (I would expect it to print garbage),
    > but, you tried to read uninitialized variable, which, I believe, is
    > undefined behavior.


    In debug mode, VS includes some runtime checks. It fills uninitialized data
    with some specific bit pattern (like 0xabababab, 0xcdcdcdcd, or 0xefefefef,
    I don't recall which one. The others are for similar cases like already
    freed data on the heap) and throws an exception when reading them.

    By examining the exception that leads to the crash, you can find out the
    reason quickly.

    Tobi
     
    Tobias Müller, Nov 22, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    952
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    447
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    465
    Ron Natalie
    Dec 11, 2005
  4. Jess
    Replies:
    23
    Views:
    938
  5. Taras_96
    Replies:
    3
    Views:
    592
    Michael Tsang
    Oct 30, 2009
Loading...

Share This Page