{"id":463278,"date":"2025-06-13T15:00:29","date_gmt":"2025-06-13T15:00:29","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=463278"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=463278","title":{"rendered":"<span>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u0432 embedded \u043c\u0438\u0440\u0435<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u0434 embedded, \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043a\u0430\u043a<code> -nostdlib -fno-exceptions -fno-rtti<\/code>. <\/p>\n<p>\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u0440\u0435\u0434\u0430\u0445 \u043d\u0435\u0442 malloc\/free (new\/delete) \u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u00ab\u0431\u043e\u043b\u044c\u0448\u0438\u0445\u00bb \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 C++ (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, std::vector) \u043d\u0435\u0440\u0435\u0434\u043a\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u043e<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u00ab\u0440\u0443\u0447\u043d\u043e\u0433\u043e\u00bb \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u041d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0432\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0430:<\/p>\n<ul>\n<li>\n<p><strong>\u0411\u0443\u0444\u0435\u0440 + \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 new (placement new)<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043a\u0443\u0447\u0430 (Heap)<\/strong><\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u0442<\/h3>\n<ol>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/strong><\/p>\n<p>\u041f\u0430\u043c\u044f\u0442\u044c (RAM\/Flash) \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441\u043e\u0442\u043d\u044f\u043c\u0438 \u043a\u0438\u043b\u043e\u0431\u0430\u0439\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0430\u0439\u0442 \u043d\u0430 \u0441\u0447\u0435\u0442\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/strong><\/p>\n<p> \u041f\u0440\u0438 \u0444\u043b\u0430\u0433\u0430\u0445 <code>-nostdlib<\/code> \u0438 <code>-fno-exceptions<\/code> \u043c\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432\u0441\u044e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 C\/C++ runtime:<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u0442 <code>new<\/code>\/<code>delete<\/code> (\u043a\u0440\u043e\u043c\u0435 placement-new, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c).<\/p>\n<\/li>\n<li>\n<p><code>malloc<\/code>\/<code>free<\/code> \u043d\u0435\u0440\u0435\u0434\u043a\u043e \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0438\u043b\u0438 \u0432\u0435\u0434\u0443\u0442 \u043a \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f\u043c (heap overflow, \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c<\/strong><br \/> \u0412\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f. \u0424\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 \u0438 \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u0435 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438 \u043d\u0435\u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u044b.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u043b\u0438\u0431\u043e <strong>\u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u00ab\u0441\u044b\u0440\u043e\u0439\u00bb \u0431\u0443\u0444\u0435\u0440<\/strong> \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c placement new, \u043b\u0438\u0431\u043e <strong>\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440 (\u043a\u0443\u0447\u0443)<\/strong> \u043d\u0430 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0443\u0447\u0430\u0441\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0421\u043f\u043e\u0441\u043e\u0431 1. \u0411\u0443\u0444\u0435\u0440 + placement new<\/strong><\/p>\n<p><strong>\u0418\u0434\u0435\u044f<\/strong><\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440 (\u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442),      \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u0438 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u044e \u043f\u043e\u0434 \u043d\u0443\u0436\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u00ab\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u00bb \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e\u0432\u0435\u0440\u0445 \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e placement      new.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 operator new(size_t), \u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0432 \u043a\u043e\u0434\u0435      \u043f\u0438\u0441\u0430\u0442\u044c new (&amp;buffer) Type(args&#8230;).<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong><\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <code>Ppo::timer<\/code>, \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0435\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0443\u0447\u0430\u0441\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"cpp\">\/\/ cgf.h #pragma once  namespace PpoDbTimer { enum PpoDbTimerEnum : int{ timer1,         TotalCount     }; }<\/code><\/pre>\n<pre><code class=\"cpp\">\/\/ ram.cpp #include \"cfg.h\"  namespace PpoDbRam {     int timers     [ PpoDbTimer::TotalCount     ] { 0 }; }<\/code><\/pre>\n<pre><code class=\"cpp\">\/\/ rom.cpp #include &lt;cstddef&gt; #include \"cfg.h\"  namespace PpoDbRam {     extern int timers[]; }  namespace Ppo {     class timer{         public:             explicit timer(int *_timer) noexcept :time(_timer) {}              void tick() noexcept {                 if (time) {                     ++(*time);                 }             }                              private:             int * const time;             \/\/\/ ...     }; }  \/*     \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 new *\/ inline void *operator new( size_t, void *ptr ) { return ptr; }  namespace {     template &lt;size_t Len, size_t Align&gt;     struct aligned_storage{         struct type{             alignas(Align) unsigned char data[Len];         };     };      aligned_storage&lt; sizeof( Ppo::timer ), alignof( Ppo::timer ) &gt;::type timer1Buf; }  void init(){    auto &amp; timer1 = *new (&amp;timer1Buf) Ppo::timer(&amp;PpoDbRam::timers[PpoDbTimer::timer1]);    timer1.tick(); }   int main(){     init();     return 0; }<\/code><\/pre>\n<h3>\u0427\u0442\u043e \u0432\u0430\u0436\u043d\u043e<\/h3>\n<ul>\n<li>\n<p><strong>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0431\u0443\u0444\u0435\u0440 <\/strong><code><strong>timer1Buf<\/strong><\/code> \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 <code>.bss<\/code> \u0438\u043b\u0438 <code>.data<\/code> (\u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438).<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435<\/strong>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>std::aligned_storage_t&lt;\u2026&gt;<\/code>, \u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>alignof(Ppo::timer)<\/code>.<\/p>\n<\/li>\n<li>\n<p><strong>Placement new<\/strong>: <code>new (raw) Ppo::timer(timerAddr)<\/code> \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 <code>operator new(size_t)<\/code>, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0443\u0436\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <\/strong><code><strong>delete<\/strong><\/code>: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f, \u043e\u0431\u044a\u0435\u043a\u0442 \u0436\u0438\u0432\u0451\u0442 \u00ab\u0432\u0435\u0447\u043d\u043e\u00bb \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>UB \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u0434\u043e <\/strong><code><strong>init()<\/strong><\/code>: \u0435\u0441\u043b\u0438 \u0433\u0434\u0435-\u0442\u043e \u043a\u043e\u0434 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <code>timer1Ptr-&gt;\u2026<\/code> \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 <code>init()<\/code>, \u0431\u0443\u0434\u0435\u0442 UB.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0421\u043f\u043e\u0441\u043e\u0431 2. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043a\u0443\u0447\u0430 (Heap)<\/h3>\n<h4>\u0418\u0434\u0435\u044f<\/h4>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <code>MyHeap<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0435\u0440\u0436\u0438\u0442:<\/p>\n<ul>\n<li>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432-\u0431\u0443\u0444\u0435\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 2 \u041c\u0411)<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0434\u0432\u0438\u0433 (offset) \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442 \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u043e.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>allocate(size_t size, size_t alignment)<\/code>:<\/p>\n<ul>\n<li>\n<p>\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u044f\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u0435\u0440\u0445 \u043f\u043e\u0434 <code>alignment<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e <code>offset + size \u2264 BUFFER_SIZE<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0443\u0441\u043f\u0435\u0445\u0435 \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>void*<\/code> \u043d\u0430 \u0432\u044b\u0440\u043e\u0432\u043d\u0435\u043d\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c <code>offset<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043d\u0435\u0445\u0432\u0430\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>nullptr<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>(\u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e) \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <strong>\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439<\/strong> <code>operator new(size_t)<\/code> \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0434\u0435\u043b\u0430\u043b <code>MyHeap::allocate(...)<\/code>. \u0418 \u0442\u043e\u0433\u0434\u0430 \u0432 \u043a\u043e\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c <code>new T(args...)<\/code>, \u0438 \u0432\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u00ab\u0431\u0440\u0430\u0442\u044c\u00bb \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430. \u041d\u043e \u0447\u0430\u0449\u0435 \u0434\u0435\u0440\u0436\u0430\u0442 <code>create&lt;T&gt;(...)<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>MyHeap<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0435\u0448\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c <code>new<\/code> \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445.<\/p>\n<\/li>\n<\/ol>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<pre><code class=\"cpp\">\/\/heap.h #pragma once  #include &lt;cstddef&gt;  class MyHeap { public:     \/\/ \u041f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0434\u0430\u0442\u044c size \u0431\u0430\u0439\u0442 \u0441 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435\u043c alignment. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 nullptr, \u0435\u0441\u043b\u0438 \u043a\u043e\u043d\u0447\u0438\u043b\u0430\u0441\u044c \u043f\u0430\u043c\u044f\u0442\u044c.     static void* allocate(size_t size, size_t alignment = alignof(size_t)) noexcept {         size_t cur = reinterpret_cast&lt;size_t&gt;(buffer) + offset;         size_t aligned = (cur + alignment - 1) &amp; ~(alignment - 1);         size_t newOffset = static_cast&lt;size_t&gt;(aligned - reinterpret_cast&lt;size_t&gt;(buffer)) + size;          if (newOffset &gt; BUFFER_SIZE)             return nullptr;          void* ptr = reinterpret_cast&lt;void*&gt;(aligned);         offset = newOffset;         return ptr;     }      template &lt;typename T, typename... Args&gt;     static T* create(Args&amp;&amp;... args) noexcept {         void* mem = allocate(sizeof(T), alignof(T));         if (!mem)              return nullptr;         return new (mem) T(static_cast&lt;Args&amp;&amp;&gt;(args)...);     } private:     static constexpr size_t BUFFER_SIZE = 2 * 1024 * 1024;     alignas(alignof(size_t)) static char buffer[];     static size_t offset; };<\/code><\/pre>\n<pre><code class=\"cpp\">\/\/ rom.cpp  #include \"heap.h\" #include \"cfg.h\" #include &lt;cstddef&gt;  namespace PpoDbRam {     extern int timers[]; }  alignas(alignof(std::max_align_t)) char MyHeap::buffer[ MyHeap::BUFFER_SIZE ]; std::size_t MyHeap::offset = 0;  \/\/ \u041e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u00abplacement new\u00bb, \u0447\u0442\u043e\u0431\u044b new(mem) T() \u0440\u0430\u0431\u043e\u0442\u0430\u043b: void* operator new(size_t, void* ptr) noexcept {     return ptr; }  \/\/ \u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 operator new(size) \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u0447\u0435\u0440\u0435\u0437 MyHeap::allocate. void* operator new(size_t size) noexcept {     return MyHeap::allocate(size, alignof(max_align_t)); }  \/\/ \u041f\u0443\u0441\u0442\u043e\u0439 operator delete (\u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f) void operator delete(void*) noexcept { }  void operator delete(void*, size_t) noexcept { }  namespace Ppo {     class timer{         public:             explicit timer(int *_timer) noexcept :time(_timer) {}              void tick() noexcept {                 if (time) {                     ++(*time);                 }             }                              private:             int * const time;             \/\/\/ ...     }; }  void init(){     auto &amp; timer1 = *new Ppo::timer(&amp;PpoDbRam::timers[ PpoDbTimer::timer1 ]);     timer1.tick(); }  int main(){     init();     return 0; } <\/code><\/pre>\n<h3>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432<\/h3>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u041a\u0440\u0438\u0442\u0435\u0440\u0438\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0421\u043f\u043e\u0441\u043e\u0431 1 (\u0431\u0443\u0444\u0435\u0440 + placement new)<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0421\u043f\u043e\u0441\u043e\u0431 2 (\u0441\u0432\u043e\u044f \u043a\u0443\u0447\u0430)<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e: \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u0441\u0432\u043e\u0439 \u0431\u0443\u0444\u0435\u0440 \u0438 <code>placement new<\/code>.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0443\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u043a\u0443\u0447\u0443, \u043c\u0435\u0442\u043e\u0434\u044b allocate\/offset, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>new\/delete<\/code> (\u0435\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f).<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0442\u0438\u043f \u0438 \u043a\u043e\u043b-\u0432\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>new T()<\/code> \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0438 \u0441\u043e\u0442\u043d\u0438 \u0440\u0430\u0437 \u0434\u043e \u0438\u0441\u0447\u0435\u0440\u043f\u0430\u043d\u0438\u044f \u0431\u0443\u0444\u0435\u0440\u0430.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0423\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u00ab\u0416\u0435\u0441\u0442\u043a\u043e\u00bb \u0437\u0430\u043a\u0440\u0435\u043f\u043b\u0451\u043d \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b, \u043a\u0443\u0447\u0443 \u00ab\u0436\u0438\u0440\u043d\u0435\u0435\u00bb (\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u0443\u0444\u0435\u0440\u0435, \u043f\u0440\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u043c \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0438).<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0420\u0430\u0437\u0440\u0435\u0437 \u00ab\u0442\u0430\u0439\u043c\u0438\u043d\u0433\u00bb<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435, \u0433\u0434\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u0431\u0443\u0444\u0435\u0440.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u043e.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041a\u043e\u0434\u043e\u0432\u0430\u044f \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u0438 N \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432\/\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u2014 N \u0431\u0443\u0444\u0435\u0440\u043e\u0432.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0434\u0438\u043d \u043a\u043b\u0430\u0441\u0441-\u043a\u0443\u0447\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0420\u0438\u0441\u043a UB \u0438 \u043e\u0448\u0438\u0431\u043e\u043a<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041c\u0430\u043b\u043e \u043a\u043e\u0434\u0430 \u2014 \u043c\u0435\u043d\u044c\u0448\u0435 \u0448\u0430\u043d\u0441\u043e\u0432 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043e\u043b\u044c\u0448\u0435 \u043a\u043e\u0434\u0430\/\u043b\u043e\u0433\u0438\u043a\u0438 \u2014 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0430 \u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0438 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u044f, offset \u0438 \u0442. \u0434.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438<\/h3>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c <strong>\u043d\u0443\u0436\u043d\u043e \u043e\u0434\u043d\u043e-\u0434\u0432\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/strong> \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>\u0421\u043f\u043e\u0441\u043e\u0431 1<\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"cpp\">static std::aligned_storage_t&lt;sizeof(MyType), alignof(MyType)&gt; myObjBuf; MyType&amp; myObj = *new (&amp;myObjBuf) MyType(arg\u2026); <\/code><\/pre>\n<p>\u042d\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0432\u044b \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u0443\u0447\u0435.<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0433\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0438 (\u0438\u043b\u0438 \u0442\u044b\u0441\u044f\u0447\u0438 \u043a\u0430\u043a \u0432 \u043c\u043e\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435) \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0432\u044b\u0431\u0438\u0440\u0430\u0439\u0442\u0435 <strong>\u0421\u043f\u043e\u0441\u043e\u0431 2<\/strong> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0439\u0442\u0435 \u0441\u0432\u043e\u044e \u043a\u0443\u0447\u0443. \u0422\u0430\u043a \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0435 <code>new T()<\/code>, \u0430 \u0432\u0441\u0435 \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u00ab\u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f\u00bb \u0432 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0412\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435 unordered_map&lt;int,array&lt;int&gt;&gt;.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 embedded-\u043c\u0438\u0440\u0435 <strong>\u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 allocator<\/strong> \u0438 \u0432\u0441\u044f \u0441\u0442\u0438\u0445\u0438\u0439\u043d\u0430\u044f \u0444\u043b\u0435\u0448-\u0440\u0430\u043c\u043a\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e C++ \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 <code>-nostdlib -fno-exceptions -fno-rtti<\/code>. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e (\u043f\u0443\u0441\u0442\u044c \u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0436\u0435\u0441\u0442\u043a\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0433\u043e) \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438, \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u0443\u0442\u0438:<\/p>\n<ol>\n<li>\n<p><strong>\u0411\u0443\u0444\u0435\u0440+placement new<\/strong> \u2014 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438 \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u043e\u0431\u044a\u0451\u043c\u044b \u0438 \u0442\u0438\u043f\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043a\u0443\u0447\u0430<\/strong> \u2014 \u0433\u0438\u0431\u043a\u0438\u0439 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0451\u043c \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u044b.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/918122\/\"> https:\/\/habr.com\/ru\/articles\/918122\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u0434 embedded, \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043a\u0430\u043a<code> -nostdlib -fno-exceptions -fno-rtti<\/code>. <\/p>\n<p>\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u0440\u0435\u0434\u0430\u0445 \u043d\u0435\u0442 malloc\/free (new\/delete) \u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u00ab\u0431\u043e\u043b\u044c\u0448\u0438\u0445\u00bb \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 C++ (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, std::vector) \u043d\u0435\u0440\u0435\u0434\u043a\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u043e<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u00ab\u0440\u0443\u0447\u043d\u043e\u0433\u043e\u00bb \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u041d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0432\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0430:<\/p>\n<ul>\n<li>\n<p><strong>\u0411\u0443\u0444\u0435\u0440 + \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 new (placement new)<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043a\u0443\u0447\u0430 (Heap)<\/strong><\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u0442<\/h3>\n<ol>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b<\/strong><\/p>\n<p>\u041f\u0430\u043c\u044f\u0442\u044c (RAM\/Flash) \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441\u043e\u0442\u043d\u044f\u043c\u0438 \u043a\u0438\u043b\u043e\u0431\u0430\u0439\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0430\u0439\u0442 \u043d\u0430 \u0441\u0447\u0435\u0442\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/strong><\/p>\n<p> \u041f\u0440\u0438 \u0444\u043b\u0430\u0433\u0430\u0445 <code>-nostdlib<\/code> \u0438 <code>-fno-exceptions<\/code> \u043c\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432\u0441\u044e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 C\/C++ runtime:<\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u0442 <code>new<\/code>\/<code>delete<\/code> (\u043a\u0440\u043e\u043c\u0435 placement-new, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c).<\/p>\n<\/li>\n<li>\n<p><code>malloc<\/code>\/<code>free<\/code> \u043d\u0435\u0440\u0435\u0434\u043a\u043e \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0438\u043b\u0438 \u0432\u0435\u0434\u0443\u0442 \u043a \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f\u043c (heap overflow, \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f).<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c<\/strong><br \/> \u0412\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f. \u0424\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 \u0438 \u043d\u0435\u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u0435 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438 \u043d\u0435\u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u044b.<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u043b\u0438\u0431\u043e <strong>\u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u00ab\u0441\u044b\u0440\u043e\u0439\u00bb \u0431\u0443\u0444\u0435\u0440<\/strong> \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c placement new, \u043b\u0438\u0431\u043e <strong>\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440 (\u043a\u0443\u0447\u0443)<\/strong> \u043d\u0430 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0443\u0447\u0430\u0441\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0421\u043f\u043e\u0441\u043e\u0431 1. \u0411\u0443\u0444\u0435\u0440 + placement new<\/strong><\/p>\n<p><strong>\u0418\u0434\u0435\u044f<\/strong><\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440 (\u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442),      \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u0438 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u044e \u043f\u043e\u0434 \u043d\u0443\u0436\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u00ab\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u00bb \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e\u0432\u0435\u0440\u0445 \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e placement      new.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 operator new(size_t), \u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0432 \u043a\u043e\u0434\u0435      \u043f\u0438\u0441\u0430\u0442\u044c new (&amp;buffer) Type(args&#8230;).<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong><\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <code>Ppo::timer<\/code>, \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0435\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0443\u0447\u0430\u0441\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"cpp\">\/\/ cgf.h #pragma once  namespace PpoDbTimer { enum PpoDbTimerEnum : int{ timer1,         TotalCount     }; }<\/code><\/pre>\n<pre><code class=\"cpp\">\/\/ ram.cpp #include \"cfg.h\"  namespace PpoDbRam {     int timers     [ PpoDbTimer::TotalCount     ] { 0 }; }<\/code><\/pre>\n<pre><code class=\"cpp\">\/\/ rom.cpp #include &lt;cstddef&gt; #include \"cfg.h\"  namespace PpoDbRam {     extern int timers[]; }  namespace Ppo {     class timer{         public:             explicit timer(int *_timer) noexcept :time(_timer) {}              void tick() noexcept {                 if (time) {                     ++(*time);                 }             }                              private:             int * const time;             \/\/\/ ...     }; }  \/*     \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 new *\/ inline void *operator new( size_t, void *ptr ) { return ptr; }  namespace {     template &lt;size_t Len, size_t Align&gt;     struct aligned_storage{         struct type{             alignas(Align) unsigned char data[Len];         };     };      aligned_storage&lt; sizeof( Ppo::timer ), alignof( Ppo::timer ) &gt;::type timer1Buf; }  void init(){    auto &amp; timer1 = *new (&amp;timer1Buf) Ppo::timer(&amp;PpoDbRam::timers[PpoDbTimer::timer1]);    timer1.tick(); }   int main(){     init();     return 0; }<\/code><\/pre>\n<h3>\u0427\u0442\u043e \u0432\u0430\u0436\u043d\u043e<\/h3>\n<ul>\n<li>\n<p><strong>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0431\u0443\u0444\u0435\u0440 <\/strong><code><strong>timer1Buf<\/strong><\/code> \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 <code>.bss<\/code> \u0438\u043b\u0438 <code>.data<\/code> (\u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438).<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435<\/strong>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>std::aligned_storage_t&lt;\u2026&gt;<\/code>, \u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>alignof(Ppo::timer)<\/code>.<\/p>\n<\/li>\n<li>\n<p><strong>Placement new<\/strong>: <code>new (raw) Ppo::timer(timerAddr)<\/code> \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 <code>operator new(size_t)<\/code>, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0443\u0436\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 <\/strong><code><strong>delete<\/strong><\/code>: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f, \u043e\u0431\u044a\u0435\u043a\u0442 \u0436\u0438\u0432\u0451\u0442 \u00ab\u0432\u0435\u0447\u043d\u043e\u00bb \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>UB \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u0434\u043e <\/strong><code><strong>init()<\/strong><\/code>: \u0435\u0441\u043b\u0438 \u0433\u0434\u0435-\u0442\u043e \u043a\u043e\u0434 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <code>timer1Ptr-&gt;\u2026<\/code> \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 <code>init()<\/code>, \u0431\u0443\u0434\u0435\u0442 UB.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0421\u043f\u043e\u0441\u043e\u0431 2. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043a\u0443\u0447\u0430 (Heap)<\/h3>\n<h4>\u0418\u0434\u0435\u044f<\/h4>\n<ol>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 <code>MyHeap<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0435\u0440\u0436\u0438\u0442:<\/p>\n<ul>\n<li>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432-\u0431\u0443\u0444\u0435\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, 2 \u041c\u0411)<\/p>\n<\/li>\n<li>\n<p>\u0422\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0434\u0432\u0438\u0433 (offset) \u2014 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442 \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u043e.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>allocate(size_t size, size_t alignment)<\/code>:<\/p>\n<ul>\n<li>\n<p>\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u044f\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u0435\u0440\u0445 \u043f\u043e\u0434 <code>alignment<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e <code>offset + size \u2264 BUFFER_SIZE<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u0443\u0441\u043f\u0435\u0445\u0435 \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>void*<\/code> \u043d\u0430 \u0432\u044b\u0440\u043e\u0432\u043d\u0435\u043d\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c <code>offset<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043d\u0435\u0445\u0432\u0430\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>nullptr<\/code>.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>(\u041e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e) \u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <strong>\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439<\/strong> <code>operator new(size_t)<\/code> \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0434\u0435\u043b\u0430\u043b <code>MyHeap::allocate(...)<\/code>. \u0418 \u0442\u043e\u0433\u0434\u0430 \u0432 \u043a\u043e\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c <code>new T(args...)<\/code>, \u0438 \u0432\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u00ab\u0431\u0440\u0430\u0442\u044c\u00bb \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430. \u041d\u043e \u0447\u0430\u0449\u0435 \u0434\u0435\u0440\u0436\u0430\u0442 <code>create&lt;T&gt;(...)<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>MyHeap<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0435\u0448\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c <code>new<\/code> \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445.<\/p>\n<\/li>\n<\/ol>\n<h4>\u041f\u0440\u0438\u043c\u0435\u0440<\/h4>\n<pre><code class=\"cpp\">\/\/heap.h #pragma once  #include &lt;cstddef&gt;  class MyHeap { public:     \/\/ \u041f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0434\u0430\u0442\u044c size \u0431\u0430\u0439\u0442 \u0441 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435\u043c alignment. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 nullptr, \u0435\u0441\u043b\u0438 \u043a\u043e\u043d\u0447\u0438\u043b\u0430\u0441\u044c \u043f\u0430\u043c\u044f\u0442\u044c.     static void* allocate(size_t size, size_t alignment = alignof(size_t)) noexcept {         size_t cur = reinterpret_cast&lt;size_t&gt;(buffer) + offset;         size_t aligned = (cur + alignment - 1) &amp; ~(alignment - 1);         size_t newOffset = static_cast&lt;size_t&gt;(aligned - reinterpret_cast&lt;size_t&gt;(buffer)) + size;          if (newOffset &gt; BUFFER_SIZE)             return nullptr;          void* ptr = reinterpret_cast&lt;void*&gt;(aligned);         offset = newOffset;         return ptr;     }      template &lt;typename T, typename... Args&gt;     static T* create(Args&amp;&amp;... args) noexcept {         void* mem = allocate(sizeof(T), alignof(T));         if (!mem)              return nullptr;         return new (mem) T(static_cast&lt;Args&amp;&amp;&gt;(args)...);     } private:     static constexpr size_t BUFFER_SIZE = 2 * 1024 * 1024;     alignas(alignof(size_t)) static char buffer[];     static size_t offset; };<\/code><\/pre>\n<pre><code class=\"cpp\">\/\/ rom.cpp  #include \"heap.h\" #include \"cfg.h\" #include &lt;cstddef&gt;  namespace PpoDbRam {     extern int timers[]; }  alignas(alignof(std::max_align_t)) char MyHeap::buffer[ MyHeap::BUFFER_SIZE ]; std::size_t MyHeap::offset = 0;  \/\/ \u041e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u00abplacement new\u00bb, \u0447\u0442\u043e\u0431\u044b new(mem) T() \u0440\u0430\u0431\u043e\u0442\u0430\u043b: void* operator new(size_t, void* ptr) noexcept {     return ptr; }  \/\/ \u0413\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 operator new(size) \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u0447\u0435\u0440\u0435\u0437 MyHeap::allocate. void* operator new(size_t size) noexcept {     return MyHeap::allocate(size, alignof(max_align_t)); }  \/\/ \u041f\u0443\u0441\u0442\u043e\u0439 operator delete (\u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f) void operator delete(void*) noexcept { }  void operator delete(void*, size_t) noexcept { }  namespace Ppo {     class timer{         public:             explicit timer(int *_timer) noexcept :time(_timer) {}              void tick() noexcept {                 if (time) {                     ++(*time);                 }             }                              private:             int * const time;             \/\/\/ ...     }; }  void init(){     auto &amp; timer1 = *new Ppo::timer(&amp;PpoDbRam::timers[ PpoDbTimer::timer1 ]);     timer1.tick(); }  int main(){     init();     return 0; } <\/code><\/pre>\n<h3>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432<\/h3>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u041a\u0440\u0438\u0442\u0435\u0440\u0438\u0439<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0421\u043f\u043e\u0441\u043e\u0431 1 (\u0431\u0443\u0444\u0435\u0440 + placement new)<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u0421\u043f\u043e\u0441\u043e\u0431 2 (\u0441\u0432\u043e\u044f \u043a\u0443\u0447\u0430)<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e: \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u0441\u0432\u043e\u0439 \u0431\u0443\u0444\u0435\u0440 \u0438 <code>placement new<\/code>.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0443\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u043a\u0443\u0447\u0443, \u043c\u0435\u0442\u043e\u0434\u044b allocate\/offset, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>new\/delete<\/code> (\u0435\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f).<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0442\u0438\u043f \u0438 \u043a\u043e\u043b-\u0432\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>new T()<\/code> \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0438 \u0441\u043e\u0442\u043d\u0438 \u0440\u0430\u0437 \u0434\u043e \u0438\u0441\u0447\u0435\u0440\u043f\u0430\u043d\u0438\u044f \u0431\u0443\u0444\u0435\u0440\u0430.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0423\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u00ab\u0416\u0435\u0441\u0442\u043a\u043e\u00bb \u0437\u0430\u043a\u0440\u0435\u043f\u043b\u0451\u043d \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b, \u043a\u0443\u0447\u0443 \u00ab\u0436\u0438\u0440\u043d\u0435\u0435\u00bb (\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u0443\u0444\u0435\u0440\u0435, \u043f\u0440\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u043c \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0438).<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0420\u0430\u0437\u0440\u0435\u0437 \u00ab\u0442\u0430\u0439\u043c\u0438\u043d\u0433\u00bb<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435, \u0433\u0434\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d \u0431\u0443\u0444\u0435\u0440.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u043e.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041a\u043e\u0434\u043e\u0432\u0430\u044f \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041f\u0440\u0438 N \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432\/\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u2014 N \u0431\u0443\u0444\u0435\u0440\u043e\u0432.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0434\u0438\u043d \u043a\u043b\u0430\u0441\u0441-\u043a\u0443\u0447\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0420\u0438\u0441\u043a UB \u0438 \u043e\u0448\u0438\u0431\u043e\u043a<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041c\u0430\u043b\u043e \u043a\u043e\u0434\u0430 \u2014 \u043c\u0435\u043d\u044c\u0448\u0435 \u0448\u0430\u043d\u0441\u043e\u0432 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0411\u043e\u043b\u044c\u0448\u0435 \u043a\u043e\u0434\u0430\/\u043b\u043e\u0433\u0438\u043a\u0438 \u2014 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0430 \u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0438 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u044f, offset \u0438 \u0442. \u0434.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h3>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438<\/h3>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c <strong>\u043d\u0443\u0436\u043d\u043e \u043e\u0434\u043d\u043e-\u0434\u0432\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/strong> \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u043f\u0440\u043e\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <strong>\u0421\u043f\u043e\u0441\u043e\u0431 1<\/strong>:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"cpp\">static std::aligned_storage_t&lt;sizeof(MyType), alignof(MyType)&gt; myObjBuf; MyType&amp; myObj = *new (&amp;myObjBuf) MyType(arg\u2026); <\/code><\/pre>\n<p>\u042d\u0442\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0432\u044b \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u0443\u0447\u0435.<\/p>\n<ul>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0433\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u043a\u0438 (\u0438\u043b\u0438 \u0442\u044b\u0441\u044f\u0447\u0438 \u043a\u0430\u043a \u0432 \u043c\u043e\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435) \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0432\u044b\u0431\u0438\u0440\u0430\u0439\u0442\u0435 <strong>\u0421\u043f\u043e\u0441\u043e\u0431 2<\/strong> \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0439\u0442\u0435 \u0441\u0432\u043e\u044e \u043a\u0443\u0447\u0443. \u0422\u0430\u043a \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0435 <code>new T()<\/code>, \u0430 \u0432\u0441\u0435 \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u00ab\u0443\u043f\u0430\u043a\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f\u00bb \u0432 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0435\u0440. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0412\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435 unordered_map&lt;int,array&lt;int&gt;&gt;.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 embedded-\u043c\u0438\u0440\u0435 <strong>\u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 allocator<\/strong> \u0438 \u0432\u0441\u044f \u0441\u0442\u0438\u0445\u0438\u0439\u043d\u0430\u044f \u0444\u043b\u0435\u0448-\u0440\u0430\u043c\u043a\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e C++ \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 <code>-nostdlib -fno-exceptions -fno-rtti<\/code>. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e (\u043f\u0443\u0441\u0442\u044c \u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0436\u0435\u0441\u0442\u043a\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0433\u043e) \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438, \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u0443\u0442\u0438:<\/p>\n<ol>\n<li>\n<p><strong>\u0411\u0443\u0444\u0435\u0440+placement new<\/strong> \u2014 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438 \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0433\u0434\u0430 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u043e\u0431\u044a\u0451\u043c\u044b \u0438 \u0442\u0438\u043f\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043a\u0443\u0447\u0430<\/strong> \u2014 \u0433\u0438\u0431\u043a\u0438\u0439 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0451\u043c \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u044b.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/918122\/\"> https:\/\/habr.com\/ru\/articles\/918122\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-463278","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=463278"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/463278\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=463278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=463278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}