c++ - Remove delete/delete[] -
i'm trying remove delete , delete[] of old application , use smart pointers instead. in following code snippet, want remove last cicle.
std::unique_ptr<mapifiledesc> filedesc(new mapifiledesc[numfiles]); (int = 0; < numfiles; ++i) { // works i've delete[] @ end filedesc[i].lpszpathname = new char[max_path]; // not work. each iteration previous array deleted // happens shared_array boost::scoped_array<char> pathname(new char[max_path]); filedesc[i].lpszpathname = pathname.get(); } // want remove following cicle (int = 0; < numfiles; ++i) { delete [] filedesc[i].lpszpathname; filedesc[i].lpszpathname = nullptr; }
what think best approach situation: use wrapper object keep track of arrays created , delete them in destructor or use vector of boost::shared_array , assign them each of elements?
std::vector<boost::shared_array<char> > objs; (int = 0; < 10; ++i) { objs.push_back(boost::shared_array<char>(new char[max_path])); }
i need use boost::shared_ptr since i'm using vc++ 2008
thanks in advance. j. lacerda
std::vector<std::string > objs(numfiles, std::string(max_path, 0)); std::vector<mapifiledesc> filedesc(numfiles); (int = 0; < numfiles; ++i) filedesc[i].lpszpathname=objs[i].data(); // after c api calls, if need use strings c++ strings, // resync c++ string length c string data // (not necessary if use them via c_str()) (int = 0; < numfiles; ++i) objs[i].resize(strlen(objs[i].c_str());
by way, if don't need pass whole array c apis single structs, can make single vector of struct stores both mapifiledesc
structure , std::string
, binding lifetime of both objects , allowing constructor take care of linking lpszpathname
string data()
member; still, wouldn't bother if structure used in single function.
Comments
Post a Comment