I have
std::ostringstream out;
out << name << len << data;
std::string const value = out.str();
I want to copy this const value to a char*
char* buff;
size_t length = strlen(value);
buff = new char[(strlen(value))+ 1];
memcpy(nm, value , length + 1);
but it is still giving me error? Can you please tell me what I am doing wrong? Thank you.
Use
value.size()instead ofstrlen(value).Or you can copy in this as well:
But note that
buffwill exist as long as the variablecopyexists. In other words, the lifetime ofbuffis tied with the lifetime of the variablecopy, and you don’t need to writedelete buffin this case.