CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2006
    Posts
    5

    default initialization of basic types in c++

    What is the default initialization of basic data types such as int, char, pointer, array in different declarations such as when declared on function stack, in class, function static, class static, global, on thread stack, etc. ?

    thanks,

    Sam

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: default initialization of basic types in c++

    There is no default initialization on basic (POD) types. Only in classes via constructor.

  3. #3
    Join Date
    Oct 2006
    Posts
    5

    Re: default initialization of basic types in c++

    Quote Originally Posted by hoxsiew View Post
    There is no default initialization on basic (POD) types. Only in classes via constructor.
    Consider this class.

    Code:
    class A {
    public:
      int d1;
      char d2;
      float d3;
      int *d4;
    };
    
    int main() {
      A a;
      cout << a.d1 << " " << (int)a.d2 << " " << a.d3 << " " << a.d4;
    
      return 0;
    }
    The default constructor should initialize the basic data types but it doesn't. This prints random values, e.g.
    2281060 -24 1.63647e+20 0x6116b71a
    Last edited by samitj; December 19th, 2009 at 03:33 PM. Reason: code

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: default initialization of basic types in c++

    Quote Originally Posted by samitj View Post
    The default constructor should initialize the basic data types...
    Where did you get this idea?

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: default initialization of basic types in c++

    No, the constructor does nothing of the sort. C/C++ is designed to be FAST and do exactly what you tell it to do. If you don't tell it to do something, then it doesn't. Are you coming from a java / basic / C# background? In those languages, numbers initialize to zero, not in C++, the memory is just allocated, so whatever was stored at that address previous is what is stored in the variable.

    You can create a constructor that initializes those values if you want, but it doesn't happen automatically.

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: default initialization of basic types in c++

    Quote Originally Posted by samitj
    The default constructor should initialize the basic data types but it doesn't. This prints random values, e.g.
    2281060 -24 1.63647e+20 0x6116b71a
    that's because object a is defined inside main,
    any variables defined inside the function body is uninitialized,
    class members of built-in types and compound types obey the same the rule,
    and are only default initialized for the object that are defined outside the function body.
    Code:
    class A {
    public:
      int d1;
      char d2;
      float d3;
      int *d4;
    };
    
      A a; // now global
    
    int main() {
    
      cout << a.d1 << " " << (int)a.d2 << " " << a.d3 << " " << a.d4;
    
      return 0;
    }
    all the members of class A are default intiailized.
    Quote Originally Posted by hoxsiew
    There is no default initialization on basic (POD) types. Only in classes via constructor.
    Yes there is.
    Quote Originally Posted by ninja9578 View Post
    No, the constructor does nothing of the sort.
    Does nothing of what sort? You surely don't mean member initialization now, do you?

    Edit:
    Quote Originally Posted by ninja9578
    Are you coming from a java / basic / C# background?
    are you yourself coming from long years of C? takin your confidence gained over years
    and just did a study on the difference between the two?
    If so, you might want to consider learning C++ the right way from the start.
    (which wouldn't be too difficult for you anyway)
    I don't mean this in any degrading way, so please don't take it the wrong way.
    Last edited by potatoCode; December 20th, 2009 at 03:11 AM.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: default initialization of basic types in c++

    Quote Originally Posted by potatoCode
    that's because object a is defined inside main,
    any variables defined inside the function body is uninitialized,
    class members of built-in types and compound types obey the same the rule,
    and are only default initialized for the object that are defined outside the function body.
    More accurately, if no initialiser is provided for an object of a POD type without static storage duration, then the object has (and its members, if any, have) an indeterminate initial value.

    Quote Originally Posted by potatoCode
    all the members of class A are default intiailized.
    I think that it would be more accurate to say that they are zero initialised, although default initialisation for objects of built-in and pointer types is zero initialisation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: default initialization of basic types in c++

    How are you laserlight?

    You explain it 10 times better than me (that's probably you know that much more )
    But watch out buddy
    I'll be "tailgating" you like you're caught in the worst traffic!

  9. #9
    Join Date
    Oct 2008
    Posts
    1,456

    Re: default initialization of basic types in c++

    Furthermore, it's worth noting that the upcoming C++ standard defines the new "value-initialization" concept and that ( with the new initializer lists feature ) there will be a uniform expression for value initializing any object of any default constructible type: "T x = {};"

  10. #10
    Join Date
    Jan 2009
    Posts
    1,689

    Re: default initialization of basic types in c++

    Quote Originally Posted by potatoCode View Post
    Does nothing of what sort? You surely don't mean member initialization now, do you?
    Of course, this thread is about the basic types, constructors initialize classes, which aren't basic types.

    are you yourself coming from long years of C? takin your confidence gained over years
    and just did a study on the difference between the two?
    If so, you might want to consider learning C++ the right way from the start.
    (which wouldn't be too difficult for you anyway)
    I don't mean this in any degrading way, so please don't take it the wrong way.
    Yes, and I've been studying the difference between the two and gotten much better at doing things in a C++-way. I was just asking why the poster thought that basic types get initialized, I know that the three languages that I mention do that for him. Taken that I know all three, I would be able to help him know the major differences between them.

  11. #11
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: default initialization of basic types in c++

    Here is an example to supplement what laserlight and hoxsiew were pointing out. Default/zero initialization of basic types occcurs when you use the constructor initializer. I think that this is a misunderstood concept. Most people would probably just put the zero in the parens just to be sure but according to the std that isn't necessary if you do it like this.

    Code:
    class A {
    public:
      A() : d1(), d2(), d3(), d4()
      {
      }
      int d1;
      char d2;
      float d3;
      int *d4;
    };
    
      A a; // now global
    
    int main() {
    
      std::cout << a.d1 << " " << (int)a.d2 << " " << a.d3 << " " << a.d4 << std::endl;
    
      return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured