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?
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);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With