Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does make_unique value initializes char array

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?

like image 877
Abhinav Gauniyal Avatar asked Feb 09 '17 15:02

Abhinav Gauniyal


People also ask

How do you initialize a char array to 0?

char ZEROARRAY[1024] = {0}; The compiler would fill the unwritten entries with zeros. Alternatively you could use memset to initialize the array at program startup: memset(ZEROARRAY, 0, 1024);

What is the use of make_ unique in c++?

Using the 'make_unique' function to create the pointer helps solve this problem by guaranteeing the freeing of memory if an exception occurs. The C++17 standard, while still not specifying the exact evaluation order for arguments, provides additional guarantees.


1 Answers

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.

like image 199
Nicol Bolas Avatar answered Oct 14 '22 06:10

Nicol Bolas