does make_unique value initializes char array

Abhinav Gauniyal picture Abhinav Gauniyal · Feb 9, 2017 · Viewed 8.2k times · Source

For example -

#include <memory>

int main(){
    const auto bufSize = 1024;
    auto buffer = std::make_unique<char[]>(bufSize);
}

Is the buffer here already filled with '\0' characters or will I have to manually fill it to avoid garbage values.

And what would be the possible way to do this, will std::memset(&buffer.get(), 0, bufSize) suffice?

Answer

Nicol Bolas picture Nicol Bolas · Feb 9, 2017

All of the make_* functions use value-initialization for the type if you don't provide constructor parameters. Since the array-form of make_unique doesn't take any parameters, it will zero-out the elements.