, Align Technology. ++.
, . , ++ . , — .
.
new
delete
. , . , ++
new
delete
.
, , . , . , .
, , - .
behappy.me
:
new
delete
. , . , .
:
- .
- ,
- c
delete
; -
new
make
-; -
new
.
- c
- .
,
new
delete
STL boost.
new
delete
?
new
delete
++ .
, . , . thread-local , . . storage duration.
, . , , . .
. ( ). :
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);
API *. , . , ..
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
.
, - . , *, (. ). .
, - , : , , , - . , , — .
**, . , , . , .
, , Qt. , . , ..
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
:
- , .. , . We Know Where You Live.
make- , :
-
make
-:
-
deleter
. , ..make
-new
. -
braced initializer
, , perfect forwarding (. Effective Modern C++, Item 30).
-
-
std::make_shared
:
- (
std::weak_pointer
). -
new
delete
. - (false sharing) (. StackOverflow).
- (
. ,
delete
,
make
-
new
. ,
new
,
delete
.
,
make
- (a.k.a. STL). Don’t Help the Compiler:
. , (reference counting) - (parent to child relationship).
new
delete
. - , - .
, . OpenSceneGraph. 3D-, ++ OpenGL.
osg::Referenced
, .
ref()
,
unref()
, .
osg::ref_ptr<T>
,
T::ref()
T::unref()
.
boost::intrusive_ptr
,
ref()
unref()
.
, OpenSceneGraph 3.0: Beginner's guide:
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
.
* , , .
MFC
, MFC. ++ Windows API. GUI Windows.
, Microsoft . .. , , .
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
.
-
, GUI. Qt — UI.
QObject
. , . ( ) .
,
new
delete
. , . , (. Qt mailing list).
, new delete Qt.
std::locale::facet
++
std::locale
. (facet), . , .
, , , new (. Notes
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
. , , , .
, , , .
C++ Russia. : , , , . , , , , , , .
PS , , : «» «». , «», . , «». , : «». -. , «», .
- Herb Sutter, GotW #89 Solution: Smart Pointers.
- Scott Meyers, Effective Modern C++, Item 21, p. 139.
- Stephan T. Lavavej, Don’t Help the Compiler.
- Bjarne Stroustrup, The C++ Programming Language, 11.2.1, p. 281.
- Five Popular Myths about C++., Part 2
- Mikhail Matrosov, C++ without new and delete.