I would suggest std::vector<char>
:
std::vector<char> recvmsg()
{
std::vector<char> buffer(1024);
//..
return buffer;
}
int main()
{
std::vector<char> reply = recvmsg();
}
And then if you ever need char*
in your code, then you can use &reply[0]
anytime you want. For example,
void f(const char* data, size_t size) {}
f(&reply[0], reply.size());
And you're done. That means, if you're using C API, then you can still use std::vector
, as you can pass &reply[0]
to the C API (as shown above), and reply
to C++ API.
The bottomline is : avoid using new
as much as possible. If you use new
, then you've to manage it yourself, and you've to delete
when you don't need it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…