Can you please confirm this?
The output is undefined under the latest MinGW version.
Code:int* arr = new int[5](); cout << *arr << endl;
Can you please confirm this?
The output is undefined under the latest MinGW version.
Code:int* arr = new int[5](); cout << *arr << endl;
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.
Bah!
Wrong forum. Sorry.
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.
>>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.
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.
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.Originally Posted by Tonto
try:
int* i = new int();
int* x = new int;
Could you please elaborate Ken?Originally Posted by Ken Fitlike
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.
>> 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?
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 ().Originally Posted by Tonto
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.
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.Originally Posted by Tonto
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.
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.*
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.
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.
Oh! Excellent source that edit Dave. Thanks!
That explains it.
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.
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).Originally Posted by Mario F.
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Well, of course, I found I was unable to do
And the google thread was very helpful.Code:string * s = new string[5]("test");
>> 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.
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.
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.