MinGW doesn't value-initialize dynamic arrays of built-in type?

This is a discussion on MinGW doesn't value-initialize dynamic arrays of built-in type? within the C++ Programming forums, part of the General Programming Boards category; Can you please confirm this? The output is undefined under the latest MinGW version. Code: int* arr = new int[5](); ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467

    MinGW doesn't value-initialize dynamic arrays of built-in type?

    Can you please confirm this?

    The output is undefined under the latest MinGW version.

    Code:
    int* arr = new int[5]();
    cout << *arr << endl;
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Bah!
    Wrong forum. Sorry.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>latest MinGW version<<

    This is ambiguous.

    >>Can you please confirm this?<<

    With g++.exe (GCC) 3.4.5 (mingw special), yes; I wouldn't expect it to do otherwise.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Can I ask what those () do in that case? I didn't think you could specify an initializer, and VC++ 2005 warns me if I try to specify an initialization value that I need curly braces for an array initializer (which gives more errors if I try). Sidenote: VC++ 2005 also 0 initialized the value which the () there.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Quote Originally Posted by Tonto
    Can I ask what those () do in that case?
    They value-initialize the dynamic object. They behave much like a default constructor. For built-in types, they request the compiler to initialize the object as if it had been defined globally. An int gets initialized to 0, for instance.

    try:
    int* i = new int();
    int* x = new int;

    Quote Originally Posted by Ken Fitlike
    I wouldn't expect it to do otherwise.
    Could you please elaborate Ken?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> They behave much like a default constructor. For built-in types, they request the compiler to initialize the object as if it had been defined globally

    Why does it have this functionality, but does not allow an initializer or aggregate initialization (don't know the term?) like java

    Also, if they are initialized as they would be if they were globally, isn't that like static zeroed memory? For non-built in types, is it zeroed also?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Quote Originally Posted by Tonto
    Why does it have this functionality, but does not allow an initializer or aggregate initialization (don't know the term?) like java
    It allows an initializer if the object as that constructor defined. Obviously built-in types don't. That's why you can't initialize them with anything other than ().

    But if on the other hand you are initializing a class, then you can use one of the constructors defined for that class. Take std::string for instance... or one of your user-defined classes.


    Quote Originally Posted by Tonto
    Also, if they are initialized as they would be if they were globally, isn't that like static zeroed memory? For non-built in types, is it zeroed also?
    For non-built in types, I replied above. It gets initialized according to the constructor definition. For built-in types, it depends on the type and, i'm pretty sure, on the compiler. On my implementation a bool gets initialized to 0 and a char to a space char.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    My curiousity led me to this interesing post from a related thread.

    [edit]http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20427
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Well, in the end what matters essentially is that it seems this is compiler defined for arrays of built-in type. So, the best is to avoid it. Even though the way I'm reading ISO 14882 at 8.5 seems to give credit to my initial claim that the array should be zero initialized.

    As for non arrays I think the norm is to zero initialize objects with this syntax. So I may rely on it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Oh! Excellent source that edit Dave. Thanks!

    That explains it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Mario F.
    Could you please elaborate Ken?
    Sorry, Mario, I skimmed your code and didn't notice the (); ironic given my comment about ambiguity. To be honest I wasn't aware of that feature; for what it's worth it is implemented in linux gcc (v3.3, anyway).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, of course, I found I was unable to do

    Code:
    string * s = new string[5]("test");
    And the google thread was very helpful.

    >> On my implementation a bool gets initialized to 0 and a char to a space char.

    I would hope it's not actually a space. I think NULL values are just printed as a space.

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Yup, I was wrong on that particular case. The parenthesis need to be empty for array types.

    However, needs to be said that any constructor that behaves like a default constructor will default-initialize.

    Code:
    class myClass {
       public:
          myClass(int val = 6, std::string &str = "Hello"): val_(val), str_(str) {}
       private:
          int val_;
          std::string str_;
    };
    
    // and on main()...
    myClass = new myClass[5] ();  // all elements will be default-initialized according
                                  // to de default constructor above.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. initialize dynamic arrays in contructors??
    By samual_van in forum C++ Programming
    Replies: 11
    Last Post: 05-03-2005, 08:29 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21