1

I had the following code in VC++ 2010:

PWCHAR pszErrorMessage = new WCHAR[100];

This code initializes a char pointer to an array. But the values in the array are garbage. I wanted a way to set the values to zero. Changing the above to the one below sets all the values in the array to zero. This works for arrays of custom structures too.

PWCHAR pszErrorMessage = new WCHAR[100]();
  1. Is this correct?
  2. Does this have any performance implications?
  3. Has this type of array initialization been part of VC++ 2005?
  4. Which method is been internally called to set the values of the struct in the array to zero?
Ganesh R.
  • 4,337
  • 3
  • 30
  • 46
  • 1
    Your primary question is nearly a dupe of [this one](http://stackoverflow.com/q/620137/179910), but that doesn't cover some of the implementation details you ask about. – Jerry Coffin Dec 23 '14 at 18:04
  • @JerryCoffin I think the answer you linked seems to answer the main question I had. Whether adding () zero initializes the values. I assume VC++ 2010 is C++ 2003 conformant compiler? – Ganesh R. Dec 23 '14 at 18:10
  • VC++ 2010 conforms with C++2003 in this respect (but not in some others). – Jerry Coffin Dec 23 '14 at 18:24
  • Every modern compiler optimizes this to its [memset equivalent](http://goo.gl/zQoxsy) - you can choose different compilers there and compare `foo` and `bar` disassembly. A quick test shows Visual Studio 2013 does this as well. So this is really should be a preferred way of allocating default initialized array. – dewaffled Dec 23 '14 at 18:42
  • @frymode: No, this should *not* be the preferred method of doing anything. The vast majority of the time, `std::vector ErrorMessage(100);` or just `std::string ErrorMessage;` is preferable to fiddling with raw arrays, raw `new`, and so on. – Jerry Coffin Dec 23 '14 at 18:52
  • @JerryCoffin Of course standard containers usage should be preferred when possible. I meant, this should be preferred when explicitly allocating array with `new`. – dewaffled Dec 23 '14 at 18:56
  • @frymode: The point is, there's basically *never* a situation in which the array form of `new` should be used at all. – Jerry Coffin Dec 23 '14 at 18:58
  • @JerryCoffin, Absolutely agree, my point was one should not be afraid of possible ineffective value-by-value default initialization and should use this syntax when manually allocating array. – dewaffled Dec 23 '14 at 19:04

1 Answers1

1

As noted elsewhere, yes, the parentheses force value initialization, which means arithmetic types will be initialized to zero (and pointers to null pointers, etc.) For types that explicitly define default constructors that initialize the members, this won't make any difference--for them, the default constructor will be invoked whether the parentheses are included or not.

Yes, this can have some (minor) performance implication: initializing the memory can take some time, especially if you're allocating a large amount. It doesn't always though: if you were allocating an object type with a default ctor that initialized its members, then that ctor would be used either way.

This feature was added in the C++03 standard. Offhand, I don't recall whether it was implemented in VC++ 2005 or not. I tried to do a quick scan through the VC++ developers blog, but that post-dates the release of VC++ 2005. It does include some information about VC++ 2005 SP1, which doesn't seem to mention it.

At least when I've looked at the code produced, the code to zero the allocated buffer seemed to be allocated in-line, at least for simple types like char and such. For example:

xor eax, eax
mov rcx, QWORD PTR $T86268[rsp]
rep stosb
Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Are you sure zeroing memory was not optimized away by compiler in your test? For me allocating an array this way and accessing all its elements shows [call to memset](http://pastebin.com/ea2F3UYH) in disassembly window (VS 2013). – dewaffled Dec 23 '14 at 18:27
  • @frymode: Yes and no. It didn't optimize away zeroing the memory. Given the difference in compilers (I used the 64-bit compiler from VC++ 2010, and you apparently used the 32-bit compiler from VC++ 2013) some difference in generated code is fairly unsurprising. – Jerry Coffin Dec 23 '14 at 18:49