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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -