new delete. , . , ++ new delete.
new delete . , . , .delete;new make-;new .new delete STL boost.new delete?new delete ++ .struct A
{
std::string str; // , A (
// ). - (*),
// std::string, A.
// (*) , Small String Optimization
// .
};
void foo()
{
std::vector<int> v; // , .
v.push_back(10); // - (),
// , .
A a; // , .
A* pa = new A; // pa - , ,
// , .
delete pa; // .
auto upa = // upa - , ,
std::make_unique<A>(); // ,
// .
}
upa->str , .. — *upa. .. / , .a *pa , , — .new, delete. : , ! , . .new delete?new delete . delete:delete ( , memory leak).delete ( ).delete ( , double delete).delete delete[] ( , undefined behavior).delete (dangling pointer).delete , . new , , , , . , make-.new delete. , , , new delete.new delete: new[] delete[]. , :void DoWork(int* buffer, size_t bufSize);
delete, .void Call(size_t n)
{
int* p = new int[n];
DoWork(p, n);
delete[] p; // !
}
std::vector**. . , , . :void Call(size_t n)
{
std::vector<int> v(n); // .
DoWork(v.data(), v.size());
}
delete, +, .new delete. . , .span<int>. STL- , ( ).std::vector (!) , int* — . ! !». ++ Why C++ Sails When the Vasa Sank. , std::unique_ptr<int[]>, dynarray.QObject. , , boost::intrusive_ptr.QObject setParent() children() , boost::intrusive_ptr intrusive_ptr_add_ref intrusive_ptr_release .new delete , — .pimpl; (, ).std::locale::facet (. ).std::shared_ptr std::unique_ptr. std::weak_ptr, .. std::shared_ptr .std::auto_ptr, ++ ++17. !std::make_shared std::make_unique, .std::unique_ptr<Cookie> cookie(new Cookie(dough, sugar, cinnamon));
auto cookie = std::make_unique<Cookie>(dough, sugar, cinnamon);
make- GotW #89 Effective Modern C++, Item 21. , :make-:std::make_shared:make-:deleter. , .. make- new.braced initializer, , perfect forwarding (. Effective Modern C++, Item 30).std::make_shared:std::weak_pointer).new delete .delete, make- new. , new, delete.make- (a.k.a. STL). Don’t Help the Compiler:
new delete. - , - .
osg::Referenced, . ref() , unref() , .osg::ref_ptr<T>, T::ref() T::unref() . boost::intrusive_ptr, ref() unref() .osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
// ...
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
// ...
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setVertexArray(vertices.get());
geom->setNormalArray(normals.get());
// ...
osg::ref_ptr<T> p = new T. , std::make_unique std::make_shared std::unique_ptr std::shared_ptr, osg::make_ref osg::ref_ptr. , std::make_unique:namespace osg
{
template<typename T, typename... Args>
osg::ref_ptr<T> make_ref(Args&&... args)
{
return new T(std::forward<Args>(args)...);
}
}
auto vertices = osg::make_ref<osg::Vec3Array>();
// ...
auto normals = osg::make_ref<osg::Vec3Array>();
// ...
auto geom = osg::make_ref<osg::Geometry>();
geom->setVertexArray(vertices.get());
geom->setNormalArray(normals.get());
// ...
delete osg::Referenced::unref(), new osg::make_ref. new delete.
CDialog::PostNcDestroy(). WM_NCDESTROY — , .CMainFrame::OnBnClickedCreate() CMyDialog::PostNcDestroy().void CMainFrame::OnBnClickedCreate()
{
auto* pDialog = new CMyDialog(this);
pDialog->ShowWindow(SW_SHOW);
}
class CMyDialog : public CDialog
{
public:
CMyDialog(CWnd* pParent)
{
Create(IDD_MY_DIALOG, pParent);
}
protected:
void PostNcDestroy() override
{
CDialog::PostNcDestroy();
delete this;
}
};
new, delete. — . , PostNcDestroy(), . new, delete, . , .new delete CModelessDialog CreateModelessDialog, :class CModelessDialog : public CDialog
{
public:
CModelessDialog(UINT nIDTemplate, CWnd* pParent)
{
Create(nIDTemplate, pParent);
}
protected:
void PostNcDestroy() override
{
CDialog::PostNcDestroy();
delete this;
}
};
//
template<class Derived, typename... Args>
Derived* CreateModelessDialog(Args&&... args)
{
// static_assert , std::enable_if , SFINAE.
// .. , .
static_assert(std::is_base_of<CModelessDialog, Derived>::value,
"CreateModelessDialog should be called for descendants of CModelessDialog");
auto* pDialog = new Derived(std::forward<Args>(args)...);
pDialog->ShowWindow(SW_SHOW);
return pDialog;
}
PostNcDestroy(), delete, , new. :void CMainFrame::OnBnClickedCreate()
{
CreateModelessDialog<CMyDialog>(this);
}
class CMyDialog : public CModelessDialog
{
public:
CMyDialog(CWnd* pParent) : CModelessDialog(IDD_MY_DIALOG, pParent) {}
};
CModelessDialog. , , CMyDialog , , . .new delete.
QObject. , . ( ) .new delete . , . , (. Qt mailing list).std::locale::facet
std::locale. (facet), . , .std::locale):std::locale default;
std::locale myLocale(default, new std::codecvt_utf8<wchar_t>);
new . , , .new delete make- .new delete . , .new delete, , . , , .new delete . .make- , .new delete. , , , .Source: https://habr.com/ru/post/283352/
All Articles