c++ - Should I delete a heap-allocated string after sending it to std::string? -
say have this:
char * p = new char[10]; std::string str = p; do have delete[] p or std::string me?
do have delete[] p or std::string me?
no doesn't generally, each new must paired delete. so,
char * p = new char[10]; /* put buffer */ std::string str = p; /* stuff */ delete [] p; but wouldn't need if created array on stack:
{ char p[10]; /* put buffer */ std::string str = p; // delete [] p; // no delete required }
Comments
Post a Comment