'How do you think could it be hit on performance?'
Actually I have no idea. I suspect there should not be a serious difference in performance compared to 'memset' but at the end it will depend on compiler implementation.
The performance side is interesting though, I'll make some test in my spare time to see if there will be significant hits on performance. Thanks for the idea.
BTW: Maybe you know this, but it's important to note that this kind of initialization (with empty parentheses) is not specific for dynamic arrays and it can be useful in other situations (I've omitted this fact in my original post because the context was dynamic arrays related).
You can have a look at this useful thread as a further reference:
IC for now. Thanx for explanation. This behaviour also present in 2005 paper. Whatever this syntax is not compatible with old standard and must not touch old code. How do you think could it be hit on performance?
For example lets take a second link from your comment.
Have a look at 5.3.4 -15 (Page 92):
'If the new-initializer is of the form (), the item is value-initialized (8.5);'
The important parts of 8.5 for us are -4 and -5.
From 8.5 -5 (Page 166):
'To value-initialize an object of type T means:
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized'
My intention was not to mislead and if my post is misleading please accept my apologies. I just shared a little-known part of the C++ Standard.
I also not use this feature because this kind of syntax is not supported by old compilers (VC 6 for example) and some of my projects must be compatible with them. But for new projects on new versions of C++ compilers I believe that using this syntax is not a problem. I've tested it with VC9 and VC10 and it just worked fine. I'll test it also with GCC and if i encounter a problem I'll let you know.
Sorry for the long post, I hope now it is clear that my intention is not to mislead or to defend the usability of this feature. I knew it was a part of the standard and I just wanted to share it in this appropriate context.
Yes, it is part of C++03.
You can try it and it should work with standard conforming C++ compilers.
The disadvantage of this syntax is that it's not compatible with older compilers like VC6 for example.
Where did you get this? Standard (let's take this http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.new or this http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf 5.3.4) says nothing about such kind of behaviour. Also it's obvious that this behavior is too implicit to use it. Do not mislead young padawans..
For default initialization of dynamic sized arrays C++ provides a little-known special syntax:
int arr_size = 100; // Or some run-time dependent value
int* p = new int[arr_size](); // Empty parentheses - the array will be zeroed
This syntax allows only default initialization with empty parentheses. You cannot pass values or use expressions in the parentheses.
Last Visit: 31-Dec-99 19:00 Last Update: 28-Dec-14 4:41