{"id":304143,"date":"2020-05-24T15:00:20","date_gmt":"2020-05-24T15:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=304143"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=304143","title":{"rendered":"\u0410\u0442\u0440\u0438\u0431\u0443\u0442 cleanup"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/503536\/\">\u0426\u0438\u0442\u0430\u0442\u0430 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 GCC [1]:<br \/>  <i>\u0410\u0442\u0440\u0438\u0431\u0443\u0442 cleanup \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0438\u0437 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u042d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0451\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043a auto-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c, \u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441 static-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0442\u0438\u043f, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0435\u0441\u0442\u044c, \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u043e\u043f\u0446\u0438\u044f -fexceptions, \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f cleanup_function \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0440\u0430\u0441\u043a\u0440\u0443\u0442\u043a\u0435 \u0441\u0442\u0435\u043a\u0430, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u041e\u0442\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 cleanup \u043d\u0435 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043e\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435. \u0415\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f cleanup_function \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044f\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e.<\/i><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qi\/dr\/gz\/qidrgzjppagqdyrqn7c4ykxa2a4.jpeg\"><\/p>\n<p>  \u0410\u0442\u0440\u0438\u0431\u0443\u0442 cleanup \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430\u043c\u0438 gcc \u0438 clang.<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 cleanup \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 cleanup \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432 std::unique_ptr \u0438 std::shared_ptr \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 C.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c cleanup \u0434\u043b\u044f \u0434\u0435\u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438:<\/p>\n<pre><code class=\"cpp\">#include&lt;stdlib.h&gt; #include&lt;stdio.h&gt;  static void free_int(int **ptr)  {     free(*ptr);      printf(&quot;cleanup done\\n&quot;); }  int main() {     __attribute__((cleanup(free_int))) int *ptr_one = (int *)malloc(sizeof(int));     \/\/ do something here     return 0; }<\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 \u00abcleanup done\u00bb. \u0412\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0443\u0440\u0430.<\/p>\n<p>  \u041d\u043e \u0441\u0440\u0430\u0437\u0443 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043e\u0447\u0435\u0432\u0438\u0434\u0435\u043d \u043e\u0434\u0438\u043d \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a: \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e<\/p>\n<pre><code class=\"cpp\">__attribute__((cleanup(free_int)))<\/code><\/pre>\n<p>  \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c cleanup, \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0430 \u0443 \u043d\u0430\u0441 \u0442\u0430\u043a\u043e\u0432\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u043c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u043e\u0431\u0451\u0440\u0442\u043a\u0430:<\/p>\n<pre><code class=\"cpp\">static void free_int(int **ptr)  {     free(*ptr);      ... }<\/code><\/pre>\n<p>  \u041a \u0442\u043e\u043c\u0443 \u0436\u0435, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043b\u044e\u0431\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">static void _free(void *p) {     free(*(void**) p);     printf(&quot;cleanup done\\n&quot;);   }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438.<\/p>\n<p>  \u0412\u043e\u0442 \u0435\u0449\u0451 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043c\u0430\u043a\u0440\u043e\u0441 (\u0438\u0437 \u043a\u043e\u0434\u043e\u0432\u043e\u0439 \u0431\u0430\u0437\u044b <a href=\"https:\/\/github.com\/systemd\/systemd\/blob\/5809f340fd7e5e6c76e229059c50d92e1f57e8d8\/src\/basic\/macro.h#L361-L366\" rel=\"nofollow\">systemd<\/a>):  <\/p>\n<pre><code class=\"cpp\">#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func)                 \\         static inline void func##p(type *p) {                   \\                 if (*p)                                         \\                         func(*p);                               \\         }                                                       \\         struct __useless_struct_to_allow_trailing_semicolon__<\/code><\/pre>\n<p>  \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose); #define _cleanup_pclose_ __attribute__((cleanup(pclosep)))<\/code><\/pre>\n<p>  \u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u0432\u0441\u0451. \u0415\u0441\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438 \u043f\u043b\u044e\u0441\u043e\u0432\u044b\u0445 unique_ptr \u0438 shared_ptr \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430: <a href=\"https:\/\/github.com\/Snaipe\/libcsptr\" rel=\"nofollow\">https:\/\/github.com\/Snaipe\/libcsptr<\/a><\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u0432\u0437\u044f\u0442 \u0438\u0437 [2]):  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt; #include &lt;csptr\/smart_ptr.h&gt; #include &lt;csptr\/array.h&gt;  void print_int(void *ptr, void *meta) {     (void) meta;     \/\/ ptr points to the current element     \/\/ meta points to the array metadata (global to the array), if any.     printf(&quot;%d\\n&quot;, *(int*) ptr); }  int main(void) {     \/\/ Destructors for array types are run on every element of the     \/\/ array before destruction.     smart int *ints = unique_ptr(int[5], {5, 4, 3, 2, 1}, print_int);     \/\/ ints == {5, 4, 3, 2, 1}      \/\/ Smart arrays are length-aware     for (size_t i = 0; i &lt; array_length(ints); ++i) {         ints[i] = i + 1;     }     \/\/ ints == {1, 2, 3, 4, 5}      return 0; }<\/code><\/pre>\n<p>  \u0412\u0441\u0451 \u0447\u0443\u0434\u0435\u0441\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442!<\/p>\n<p>  \u0410 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u0443 \u044d\u0442\u043e\u0439 \u043c\u0430\u0433\u0438\u0438. \u041d\u0430\u0447\u043d\u0451\u043c \u0441 unique_ptr (\u0438 \u0437\u0430\u043e\u0434\u043d\u043e shared_ptr):<\/p>\n<pre><code class=\"cpp\"># define shared_ptr(Type, ...) smart_ptr(SHARED, Type, __VA_ARGS__) # define unique_ptr(Type, ...) smart_ptr(UNIQUE, Type, __VA_ARGS__)<\/code><\/pre>\n<p>  \u041f\u043e\u0439\u0434\u0451\u043c \u0434\u0430\u043b\u044c\u0448\u0435, \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u0430 \u043a\u0440\u043e\u043b\u0438\u0447\u044c\u044f \u043d\u043e\u0440\u0430:<\/p>\n<pre><code class=\"cpp\"># define smart_arr(Kind, Type, Length, ...)                                 \\     ({                                                                      \\         struct s_tmp {                                                      \\             CSPTR_SENTINEL_DEC                                              \\             __typeof__(__typeof__(Type)[Length]) value;                     \\             f_destructor dtor;                                              \\             struct {                                                        \\                 const void *ptr;                                            \\                 size_t size;                                                \\             } meta;                                                         \\         } args = {                                                          \\             CSPTR_SENTINEL                                                  \\             __VA_ARGS__                                                     \\         };                                                                  \\         void *var = smalloc(sizeof (Type), Length, Kind, ARGS_);            \\         if (var != NULL)                                                    \\             memcpy(var, &amp;args.value, sizeof (Type));                        \\         var;                                                                \\     })<\/code><\/pre>\n<p>  \u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u044f\u0441\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u043b\u043e\u0441\u044c, \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u043c\u0435\u0448\u0430\u043d\u0438\u043d\u0430 \u043c\u0430\u043a\u0440\u043e\u0441\u043e\u0432 \u0432 \u043b\u0443\u0447\u0448\u0438\u0445 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u044f\u0445 \u044d\u0442\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430. \u041d\u043e \u043c\u044b \u043d\u0435 \u043f\u0440\u0438\u0432\u044b\u043a\u043b\u0438 \u043e\u0442\u0441\u0442\u0443\u043f\u0430\u0442\u044c. \u0420\u0430\u0441\u043f\u0443\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043b\u0443\u0431\u043e\u043a:<\/p>\n<pre><code class=\"cpp\">define CSPTR_SENTINEL        .sentinel_ = 0, define CSPTR_SENTINEL_DEC int sentinel_; ... typedef void (*f_destructor)(void *, void *);<\/code><\/pre>\n<p>  \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443:<\/p>\n<pre><code class=\"cpp\"># define smart_arr(Kind, Type, Length, ...)                                 \\     ({                                                                      \\         struct s_tmp {                                                      \\             int sentinel_;                                                  \\             __typeof__(__typeof__(Type)[Length]) value;                     \\             void (*)(void *, void *) dtor;                                  \\             struct {                                                        \\                 const void *ptr;                                            \\                 size_t size;                                                \\             } meta;                                                         \\         } args = {                                                          \\             .sentinel_ = 0,                                                 \\             __VA_ARGS__                                                     \\         };                                                                  \\         void *var = smalloc(sizeof (Type), Length, Kind, ARGS_);            \\         if (var != NULL)                                                    \\             memcpy(var, &amp;args.value, sizeof (Type));                        \\         var;                                                                \\     })<\/code><\/pre>\n<p>  \u0438 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0442\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 sentinel_, \u043d\u0435\u043a\u043e\u0435\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (Type)[Length], \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e-\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 (&#8230;) \u0447\u0430\u0441\u0442\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u043a\u0440\u043e\u0441\u0430, \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b meta, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438. \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u044b\u0437\u043e\u0432<\/p>\n<pre><code class=\"cpp\">smalloc(sizeof (Type), Length, Kind, ARGS_);<\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 smalloc? \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0435\u0449\u0451 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438 (\u044f \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438):<\/p>\n<pre><code class=\"cpp\">enum pointer_kind {     UNIQUE,     SHARED,     ARRAY = 1 &lt;&lt; 8 }; \/\/.. typedef struct {     CSPTR_SENTINEL_DEC     size_t size;     size_t nmemb;     enum pointer_kind kind;     f_destructor dtor;     struct {         const void *data;         size_t size;     } meta; } s_smalloc_args; \/\/... __attribute__ ((malloc)) void *smalloc(s_smalloc_args *args); \/\/... #  define smalloc(...) \\     smalloc(&amp;(s_smalloc_args) { CSPTR_SENTINEL __VA_ARGS__ })<\/code><\/pre>\n<p>  \u041d\u0443, \u0437\u0430 \u044d\u0442\u043e \u043c\u044b \u0438 \u043b\u044e\u0431\u0438\u043c \u0421. \u0422\u0430\u043a\u0436\u0435 \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0435\u0441\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f (\u0441\u0432\u044f\u0442\u044b\u0435 \u043b\u044e\u0434\u0438, \u0432\u0441\u0435\u043c \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0431\u0440\u0430\u0442\u044c \u0441 \u043d\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440):<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f <i>smalloc()<\/i> \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0430\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440 (malloc (3) \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e), \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u00ab\u0443\u043c\u043d\u044b\u043c\u00bb \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c. &lt;&#8230;&gt; \u0415\u0441\u043b\u0438 size \u0440\u0430\u0432\u0435\u043d 0, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f NULL. \u0415\u0441\u043b\u0438 nmemb \u0440\u0430\u0432\u0435\u043d 0, \u0442\u043e smalloc \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442 \u0443\u043c\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 size \u0431\u0430\u0439\u0442, \u0438 \u0443\u043c\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0441\u043a\u0430\u043b\u044f\u0440\u043d\u044b\u0439, \u0435\u0441\u043b\u0438 nmemb \u043d\u0435 \u0440\u0430\u0432\u0435\u043d 0, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 size * nmemb, \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f array.  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b<\/b>                         <\/p>\n<div class=\"spoiler_text\">\u00abThe <b>smalloc<\/b>() function calls an allocator (<b>malloc<\/b> (3) by default), such that the returned pointer is a smart pointer. &lt;&#8230;&gt; If <b>size<\/b> is 0, then <b>smalloc<\/b>() returns NULL. If <b>nmemb<\/b> is 0, then <b>smalloc<\/b> shall return a smart pointer to a memory block of at least <b>size<\/b> bytes, and the smart pointer is a scalar. Otherwise, it shall return a memory block to at least <b>size * nmemb<\/b> bytes, and the smart pointer is an array.\u00bb  <\/div>\n<\/p><\/div>\n<p>  \u0412\u043e\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a smalloc:<\/p>\n<pre><code class=\"cpp\">__attribute__ ((malloc)) void *smalloc(s_smalloc_args *args) {     return (args-&gt;nmemb == 0 ? smalloc_impl : smalloc_array)(args); }<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043e\u0434 smalloc_impl, \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0441\u043a\u0430\u043b\u044f\u0440\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432. \u0414\u043b\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u044f \u043e\u0431\u044a\u0451\u043c\u0430 \u044f \u0443\u0434\u0430\u043b\u0438\u043b \u043a\u043e\u0434, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0441 shared-\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438, \u0438 \u0441\u0434\u0435\u043b\u0430\u043b \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 inline-\u043e\u0432 \u0438 \u043c\u0430\u043a\u0440\u043e\u0441\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">static void *smalloc_impl(s_smalloc_args *args) {     if (!args-&gt;size)         return NULL;      \/\/ align the sizes to the size of a word     size_t aligned_metasize = align(args-&gt;meta.size);     size_t size = align(args-&gt;size);      size_t head_size = sizeof (s_meta);     s_meta_shared *ptr = malloc(head_size + size + aligned_metasize + sizeof (size_t));      if (ptr == NULL)         return NULL;      char *shifted = (char *) ptr + head_size;     if (args-&gt;meta.size &amp;&amp; args-&gt;meta.data)         memcpy(shifted, args-&gt;meta.data, args-&gt;meta.size);      size_t *sz = (size_t *) (shifted + aligned_metasize);     *sz = head_size + aligned_metasize;      *(s_meta*) ptr = (s_meta) {         .kind = args-&gt;kind,         .dtor = args-&gt;dtor,         .ptr = sz + 1     };      return sz + 1; }<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043f\u043b\u044e\u0441 \u043d\u0435\u043a\u0438\u0439 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0442\u0438\u043f\u0430 s_meta \u043f\u043b\u044e\u0441 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 args-&gt;meta.size, \u0432\u044b\u0440\u043e\u0432\u043d\u0435\u043d\u043d\u0430\u044f \u043f\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u0441\u043b\u043e\u0432\u0430, \u043f\u043b\u044e\u0441 \u0435\u0449\u0451 \u043e\u0434\u043d\u043e \u0441\u043b\u043e\u0432\u043e (sizeof(size_t)). \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043e\u0431\u043b\u0430\u0441\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439: ptr + head_size + aligned_metasize + 1.<\/p>\n<p>  \u041f\u0443\u0441\u0442\u044c \u043c\u044b \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0442\u0438\u043f\u0430 int, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c\u0443\u044e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 42:<\/p>\n<pre><code class=\"cpp\">smart void *ptr = unique_ptr(int, 42);<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c smart \u2014 \u044d\u0442\u043e \u043c\u0430\u043a\u0440\u043e\u0441:<\/p>\n<pre><code class=\"cpp\"># define smart __attribute__ ((cleanup(sfree_stack)))<\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f sfree_stack:<\/p>\n<pre><code class=\"cpp\">CSPTR_INLINE void sfree_stack(void *ptr) {     union {         void **real_ptr;         void *ptr;     } conv;     conv.ptr = ptr;     sfree(*conv.real_ptr);     *conv.real_ptr = NULL; }<\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f sfree (\u0441 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f\u043c\u0438):<\/p>\n<pre><code class=\"cpp\">void sfree(void *ptr) {     s_meta *meta = get_meta(ptr);     dealloc_entry(meta, ptr); }<\/code><\/pre>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f dealloc_entry \u0432 \u043ec\u043d\u043e\u0432\u043d\u043e\u043c, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u043e\u0433\u043e \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0435\u0441\u043b\u0438 \u043c\u044b \u0435\u0433\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438 \u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u0445 unique_ptr, \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043d\u0435\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d \u0432 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445. \u0415\u0441\u043b\u0438 \u0435\u0433\u043e \u043d\u0435\u0442, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e free(meta).<\/p>\n<p>  \u0421\u043f\u0438\u0441\u043e\u043a \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432:<br \/>  [1] <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Common-Variable-Attributes.html\" rel=\"nofollow\">Common Variable Attributes<\/a>.<br \/>  [2] <a href=\"https:\/\/stackoverflow.com\/questions\/34574933\/a-good-and-idiomatic-way-to-use-gcc-and-clang-attribute-cleanup-and-point\" rel=\"nofollow\">A good and idiomatic way to use GCC and clang __attribute__((cleanup)) and pointer declarations<\/a>.<br \/>  [3] <a href=\"https:\/\/echorand.me\/site\/notes\/articles\/c_cleanup\/cleanup_attribute_c.html\" rel=\"nofollow\">Using the __cleanup__ variable attribute in GCC<\/a>.<\/div>\n<p> \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\/post\/503536\/\"> https:\/\/habr.com\/ru\/post\/503536\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/503536\/\">\u0426\u0438\u0442\u0430\u0442\u0430 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 GCC [1]:<br \/>  <i>\u0410\u0442\u0440\u0438\u0431\u0443\u0442 cleanup \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0438\u0437 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u042d\u0442\u043e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u0451\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043a auto-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c, \u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441 static-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0442\u0438\u043f, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0435\u0441\u0442\u044c, \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0430 \u043e\u043f\u0446\u0438\u044f -fexceptions, \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f cleanup_function \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0440\u0430\u0441\u043a\u0440\u0443\u0442\u043a\u0435 \u0441\u0442\u0435\u043a\u0430, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u041e\u0442\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442 cleanup \u043d\u0435 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043e\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435. \u0415\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f cleanup_function \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044f\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e.<\/i><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qi\/dr\/gz\/qidrgzjppagqdyrqn7c4ykxa2a4.jpeg\"><\/p>\n<p>  \u0410\u0442\u0440\u0438\u0431\u0443\u0442 cleanup \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430\u043c\u0438 gcc \u0438 clang.<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 cleanup \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u044e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 cleanup \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432 std::unique_ptr \u0438 std::shared_ptr \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 C.  <\/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-304143","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304143","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=304143"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304143\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}