strerror_s: unique_ptr and char array+delete

Hello Forum,

How do you use std::unique_ptr with strerror_s?
std::unique_ptr<char[]> pcMessage;
pcMessage = std::unique_ptr<char[]>(new char[256]);
strerror_s(pcMessage, 256, errno); // This line is bad.

If I use a char[] for strerror_s, do I have to delete it?
char buff[256];
strerror_s(buff, 256, errno);
delete[] buff; // Is this required?

Thank you.
How do you use std::unique_ptr with strerror_s?

It is usually best to avoid C++ types, ie. unique_ptr, when trying to work with a C function, ie strerror().
Also there is really no need for dynamic memory if you're using a compile constant size like you are, just use a normal statically allocated array instead.

If I use a char[] for strerror_s, do I have to delete it?

Since you didn't use new there is no reason to use delete.
 
strerror_s(pcMessage.get(), 256, errno);
Ah, thank you both.

Topic archived. No new replies allowed.