{"id":370849,"date":"2024-05-21T04:34:56","date_gmt":"2024-05-21T04:34:56","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=370849"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=370849","title":{"rendered":"<span>\u041e\u0441\u043d\u043e\u0432\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438 \u0432 C<\/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<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0ee\/301\/97f\/0ee30197fce8a0c62b3a466275b321c9.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0ee\/301\/97f\/0ee30197fce8a0c62b3a466275b321c9.png\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em> <\/p>\n<p><strong>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438<\/strong> \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f <em>\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435<\/em>, <em>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/em> \u0438 <em>\u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435<\/em> \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432. \u0412 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <strong>C \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442<\/strong>, \u044d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u043b\u0435\u0447\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438 \u0432 C. <\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h4>\n<p><code>malloc<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 <em>\u0441\u044b\u0440\u0443\u044e<\/em> \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0435\u0437 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0442\u0435\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0438 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c. \u041d\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">int *numbers = malloc(sizeof(int) * 10); if (numbers == NULL) {     \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 }<\/code><\/pre>\n<p><code>calloc<\/code> \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043d\u0443\u043b\u0435\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<pre><code class=\"cpp\">int *matrix = calloc(10, sizeof(int)); if (matrix == NULL) {     \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 }<\/code><\/pre>\n<p>\u0421 <code>realloc<\/code> \u043c\u043e\u0436\u043d\u043e <em>\u043f\u0435\u0440\u0435\u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c<\/em> \u0443\u0436\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u0438\u0437\u043c\u0435\u043d\u044f\u044f \u0435\u0435 \u0440\u0430\u0437\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">numbers = realloc(numbers, sizeof(int) * 20); if (numbers == NULL) {     \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 }<\/code><\/pre>\n<p><code>free<\/code> \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 <em>\u0432\u0435\u0440\u043d\u0443\u0442\u044c<\/em> \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0443. \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 <code>free<\/code> \u043b\u044e\u0431\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u044d\u0442\u0438\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">free(numbers); numbers = NULL; \/\/ \u043e\u0431\u043d\u0443\u043b\u044f\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438<\/code><\/pre>\n<h4>\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438 \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/h4>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b<\/strong>\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e \u044d\u0442\u0438\u043c \u0430\u0434\u0440\u0435\u0441\u0430\u043c.  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"cpp\">void hello() {   printf(\"\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!\\n\"); }  void world() {   printf(\"\u041c\u0438\u0440 C!\\n\"); }  int main() {   void (*funcPtr)();  \/\/ \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f   funcPtr = &amp;hello;   \/\/ \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hello   funcPtr();          \/\/ \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hello   funcPtr = &amp;world;   \/\/ \u0441\u043c\u0435\u043d\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e world   funcPtr();          \/\/ \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 world   return 0; }<\/code><\/pre>\n<p><strong>\u0414\u0432\u043e\u0439\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432. \u041e\u043d\u0438 \u0442\u0430\u043a\u0436\u0435 \u044e\u0437\u0430\u0431\u0435\u043b\u044c\u043d\u044b \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0432\u043e\u0439\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h> #include &lt;stdlib.h>  void allocateArray(int **arr, int size, int value) {   *arr = (int*)malloc(size * sizeof(int));   for(int i = 0; i &lt; size; i++) {     *(*arr + i) = value;   } }  int main() {   int *array = NULL;   allocateArray(&amp;array, 5, 45);  \/\/ \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432   for(int i = 0; i &lt; 5; i++) {     printf(\"%d \", array[i]);   }   free(array);  \/\/ \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c   return 0; }<\/code><\/pre>\n<h4>Valgrind \u0438 \u0441\u0430\u043d\u0438\u0442\u0430\u0439\u0437\u0435\u0440\u044b  <\/h4>\n<p>Valgrind \u0438\u0449\u0435\u0442 <strong>\u043d\u0435\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c<\/strong>, \u043e\u0448\u0438\u0431\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdlib.h>  int main() {     int *a = malloc(sizeof(int) * 10); \/\/ \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u044b\u0434\u0435\u043b\u0438\u043b\u0438 \u043f\u0430\u043c\u044f\u0442\u044c...     \/\/ ...\u0438 \u0437\u0430\u0431\u044b\u043b\u0438 \u0435\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c. \u041e\u043f\u0435\u0447\u0430\u0442\u043a\u0430 \u0438\u043b\u0438 \u0441\u0443\u0434\u044c\u0431\u0430?     return 0; }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0432 Valgrind \u0441 \u044d\u0442\u0438\u043c \u043a\u043e\u0434\u043e\u043c, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u0435\u0447\u0442\u043e \u0432\u0440\u043e\u0434\u0435:<\/p>\n<pre><code>==12345== LEAK SUMMARY: ==12345==    definitely lost: 40 bytes in 1 blocks<\/code><\/pre>\n<p><strong>\u0421\u0430\u043d\u0438\u0442\u0430\u0439\u0437\u0435\u0440\u044b<\/strong> \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043d\u0430\u043c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, <strong>\u043d\u0435 \u0432\u044b\u0445\u043e\u0434\u044f \u0438\u0437 IDE<\/strong>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizer\">AddressSanitizer<\/a>, \u043e\u0434\u0438\u043d \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"bash\">gcc -fsanitize=address -g your_program.c<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043f\u0440\u043e\u0439\u0442\u0438 \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h4>\u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0438\u0447\u0438 \u0438\u0437 C++ (\u0447\u0442\u043e?)<\/h4>\n<p>\u041a\u0430\u043a \u0432\u044b, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0437\u043d\u0430\u0435\u0442\u0435, RAII \u2014 \u044d\u0442\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0438\u0437 C++, \u0433\u0434\u0435 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0440\u0435\u0441\u0443\u0440\u0441 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0430 \u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f. &#171;\u041d\u043e \u0441\u0442\u043e\u043f,&#187; \u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b, \u2014 &#171;\u0432 C \u043d\u0435\u0442 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u0432!&#187;. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c RAII \u0434\u043b\u044f C, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0447\u0438\u0441\u0442\u043a\u0438.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdlib.h>  typedef struct {     int* array;     size_t size; } IntArray;  IntArray* IntArray_create(size_t size) {     IntArray* ia = malloc(sizeof(IntArray));     if (ia) {         ia->array = malloc(size * sizeof(int));         ia->size = size;     }     return ia; }  void IntArray_destroy(IntArray* ia) {     if (ia) {         free(ia->array);         free(ia);     } }<\/code><\/pre>\n<p><code>IntArray_create<\/code> \u0438 <code>IntArray_destroy<\/code> \u0438\u0433\u0440\u0430\u044e\u0442 \u0440\u043e\u043b\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430. <\/p>\n<p>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u2014 \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0441\u043a\u0440\u044b\u0432\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 C \u044d\u0442\u043e \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b:<\/p>\n<pre><code class=\"cpp\">typedef struct {     FILE* file; } FileResource;  FileResource* FileResource_open(const char* filename, const char* mode) {     FileResource* fr = malloc(sizeof(FileResource));     if (fr) {         fr->file = fopen(filename, mode);     }     return fr; }  void FileResource_close(FileResource* fr) {     if (fr) {         fclose(fr->file);         free(fr);     } }<\/code><\/pre>\n<h4>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c<\/h4>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pthreads#:~:text=In%20computing%2C%20POSIX%20Threads%2C%20commonly,work%20that%20overlap%20in%20time.\">pthreads<\/a>, \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0442\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d:<\/p>\n<pre><code class=\"cpp\">#include &lt;pthread.h> #include &lt;stdio.h> #include &lt;stdlib.h>  \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 void* thread_function(void* arg) {     printf(\"\u041f\u0440\u0438\u0432\u0435\u0442 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430! \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: %s\\n\", (char*)arg);     return NULL; }  int main() {     pthread_t thread_id;     char* message = \"Thread's Message\";      \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a     if(pthread_create(&amp;thread_id, NULL, thread_function, (void*)message)) {         fprintf(stderr, \"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\\n\");         return 1;     }      \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u0430     if(pthread_join(thread_id, NULL)) {         fprintf(stderr, \"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\\n\");         return 2;     }      printf(\"\u041f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0440\u0430\u0431\u043e\u0442\u0443\\n\");     return 0; }<\/code><\/pre>\n<p><code>pthread_create<\/code> \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 <code>thread_function<\/code>, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a <code>pthread_join<\/code> \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0438\u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0447\u0430\u0441\u0442\u0438 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441. \u0421\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">#include &lt;pthread.h> #include &lt;stdio.h> #include &lt;stdlib.h>  #define SIZE 1000000 #define THREADS 4  int array[SIZE]; long long sum[THREADS] = {0};  void* sum_function(void* arg) {     int thread_part = (int)arg;     int start = thread_part * (SIZE \/ THREADS);     int end = (thread_part + 1) * (SIZE \/ THREADS);      for(int i = start; i &lt; end; i++) {         sum[thread_part] += array[i];     }      return NULL; }  int main() {     pthread_t threads[THREADS];      \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430     for(int i = 0; i &lt; SIZE; i++) {         array[i] = i + 1;     }      \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u0435\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\u0430     for(int i = 0; i &lt; THREADS; i++) {         pthread_create(&amp;threads[i], NULL, sum_function, (void*)i);     }      \/\/ \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432     for(int i = 0; i &lt; THREADS; i++) {         pthread_join(threads[i], NULL);     }      \/\/ \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     long long total_sum = 0;     for(int i = 0; i &lt; THREADS; i++) {         total_sum += sum[i];     }      printf(\"\u041e\u0431\u0449\u0430\u044f \u0441\u0443\u043c\u043c\u0430: %lld\\n\", total_sum);     return 0; }<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0434\u0435\u043b\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0447\u0435\u0442\u044b\u0440\u0435 \u0447\u0430\u0441\u0442\u0438 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0441\u0443\u043c\u043c\u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<h4>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/h4>\n<p><abbr class=\"habraabbr\" title=\"\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043b\u0438\u0431\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0438\u043a\u043e\u043c, \u043b\u0438\u0431\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0432\u0441\u0435; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430\" data-title=\"&lt;p>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043b\u0438\u0431\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0438\u043a\u043e\u043c, \u043b\u0438\u0431\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0432\u0441\u0435; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430  &lt;\/p>&#187; data-abbr=&#187;\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438&#187;>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/abbr> \u0447\u0430\u0441\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0439, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 C11 \u0438\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 GCC.<\/p>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C11 \u0432\u0432\u0435\u043b \u044f\u0432\u043d\u0443\u044e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0443\u043b\u044c <code>&lt;stdatomic.h><\/code>. \u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdatomic.h> #include &lt;stdio.h> #include &lt;pthread.h>  atomic_int counter = ATOMIC_VAR_INIT(0);  void* increment(void* arg) {     for (int i = 0; i &lt; 100000; ++i) {         atomic_fetch_add_explicit(&amp;counter, 1, memory_order_relaxed);     }     return NULL; }  int main() {     pthread_t t1, t2;      if(pthread_create(&amp;t1, NULL, increment, NULL)) {         return 1;     }     if(pthread_create(&amp;t2, NULL, increment, NULL)) {         return 1;     }      pthread_join(t1, NULL);     pthread_join(t2, NULL);      printf(\"\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430: %d\\n\", atomic_load(&amp;counter));     return 0; }<\/code><\/pre>\n<p><code>atomic_int<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u0430 <code>atomic_fetch_add_explicit<\/code> \u2014 \u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. <code>memory_order_relaxed<\/code> \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0434\u0440\u0443\u0433\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c, \u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p><a href=\"https:\/\/gcc.gnu.org\/\">GCC<\/a> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 C, \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 C11, \u0438\u043b\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 <code>&lt;stdatomic.h><\/code> \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0438\u043b\u0438 \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u0430.<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u043e\u0431\u043c\u0435\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h> #include &lt;pthread.h>  int counter = 0;  void* increment(void* arg) {     for (int i = 0; i &lt; 100000; ++i) {         __sync_fetch_and_add(&amp;counter, 1);     }     return NULL; }  int main() {     pthread_t t1, t2;      if(pthread_create(&amp;t1, NULL, increment, NULL)) {         return 1;     }     if(pthread_create(&amp;t2, NULL, increment, NULL)) {         return 1;     }      pthread_join(t1, NULL);     pthread_join(t2, NULL);      printf(\"\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430: %d\\n\", counter);     return 0; }<\/code><\/pre>\n<p>\u0424\u043d\u043a\u0446\u0438\u044f <code>__sync_fetch_and_add<\/code> \u0438\u0437 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 GCC \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f\u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430. <\/p>\n<hr\/>\n<p>\u0412 \u0421 \u0435\u0441\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u0430 \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0441\u0432\u043e\u0438\u043c \u043a\u043e\u0434\u043e\u043c, \u043d\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u044d\u0442\u0438\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0438 <strong>\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c<\/strong>. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u044d\u0442\u0443 \u0441\u0432\u043e\u0431\u043e\u0434\u0443 \u0441 \u0443\u043c\u043e\u043c, \u043e\u0441\u043e\u0437\u043d\u0430\u0432\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f \u0441\u0432\u043e\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439. <\/p>\n<p><a href=\"https:\/\/otus.pw\/QZ1K\/\">\u041f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a> \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/QZ1K\/\">&#171;\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0421&#187;<\/a> \u043f\u0440\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0421 \u0438 Python.<\/p>\n<\/p>\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\/800381\/\"> https:\/\/habr.com\/ru\/articles\/800381\/<\/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<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em> <\/p>\n<p><strong>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438<\/strong> \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f <em>\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435<\/em>, <em>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/em> \u0438 <em>\u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435<\/em> \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432. \u0412 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <strong>C \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442<\/strong>, \u044d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u043b\u0435\u0447\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438 \u0432 C. <\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h4>\n<p><code>malloc<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 <em>\u0441\u044b\u0440\u0443\u044e<\/em> \u043f\u0430\u043c\u044f\u0442\u044c \u0431\u0435\u0437 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0442\u0435\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u0435\u043d \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0438 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c. \u041d\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">int *numbers = malloc(sizeof(int) * 10); if (numbers == NULL) {     \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 }<\/code><\/pre>\n<p><code>calloc<\/code> \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0438\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0433\u0434\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043d\u0443\u043b\u0435\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u043e.<\/p>\n<pre><code class=\"cpp\">int *matrix = calloc(10, sizeof(int)); if (matrix == NULL) {     \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 }<\/code><\/pre>\n<p>\u0421 <code>realloc<\/code> \u043c\u043e\u0436\u043d\u043e <em>\u043f\u0435\u0440\u0435\u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c<\/em> \u0443\u0436\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u0438\u0437\u043c\u0435\u043d\u044f\u044f \u0435\u0435 \u0440\u0430\u0437\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">numbers = realloc(numbers, sizeof(int) * 20); if (numbers == NULL) {     \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 }<\/code><\/pre>\n<p><code>free<\/code> \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 <em>\u0432\u0435\u0440\u043d\u0443\u0442\u044c<\/em> \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0443. \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 <code>free<\/code> \u043b\u044e\u0431\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u044d\u0442\u0438\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">free(numbers); numbers = NULL; \/\/ \u043e\u0431\u043d\u0443\u043b\u044f\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438<\/code><\/pre>\n<h4>\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438 \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/h4>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b<\/strong>\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e \u044d\u0442\u0438\u043c \u0430\u0434\u0440\u0435\u0441\u0430\u043c.  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"cpp\">void hello() {   printf(\"\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!\\n\"); }  void world() {   printf(\"\u041c\u0438\u0440 C!\\n\"); }  int main() {   void (*funcPtr)();  \/\/ \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f   funcPtr = &amp;hello;   \/\/ \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hello   funcPtr();          \/\/ \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hello   funcPtr = &amp;world;   \/\/ \u0441\u043c\u0435\u043d\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e world   funcPtr();          \/\/ \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 world   return 0; }<\/code><\/pre>\n<p><strong>\u0414\u0432\u043e\u0439\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/strong> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432. \u041e\u043d\u0438 \u0442\u0430\u043a\u0436\u0435 \u044e\u0437\u0430\u0431\u0435\u043b\u044c\u043d\u044b \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0432\u043e\u0439\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h> #include &lt;stdlib.h>  void allocateArray(int **arr, int size, int value) {   *arr = (int*)malloc(size * sizeof(int));   for(int i = 0; i &lt; size; i++) {     *(*arr + i) = value;   } }  int main() {   int *array = NULL;   allocateArray(&amp;array, 5, 45);  \/\/ \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432   for(int i = 0; i &lt; 5; i++) {     printf(\"%d \", array[i]);   }   free(array);  \/\/ \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c   return 0; }<\/code><\/pre>\n<h4>Valgrind \u0438 \u0441\u0430\u043d\u0438\u0442\u0430\u0439\u0437\u0435\u0440\u044b  <\/h4>\n<p>Valgrind \u0438\u0449\u0435\u0442 <strong>\u043d\u0435\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c<\/strong>, \u043e\u0448\u0438\u0431\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdlib.h>  int main() {     int *a = malloc(sizeof(int) * 10); \/\/ \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u044b\u0434\u0435\u043b\u0438\u043b\u0438 \u043f\u0430\u043c\u044f\u0442\u044c...     \/\/ ...\u0438 \u0437\u0430\u0431\u044b\u043b\u0438 \u0435\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c. \u041e\u043f\u0435\u0447\u0430\u0442\u043a\u0430 \u0438\u043b\u0438 \u0441\u0443\u0434\u044c\u0431\u0430?     return 0; }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0432 Valgrind \u0441 \u044d\u0442\u0438\u043c \u043a\u043e\u0434\u043e\u043c, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u0435\u0447\u0442\u043e \u0432\u0440\u043e\u0434\u0435:<\/p>\n<pre><code>==12345== LEAK SUMMARY: ==12345==    definitely lost: 40 bytes in 1 blocks<\/code><\/pre>\n<p><strong>\u0421\u0430\u043d\u0438\u0442\u0430\u0439\u0437\u0435\u0440\u044b<\/strong> \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043d\u0430\u043c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, <strong>\u043d\u0435 \u0432\u044b\u0445\u043e\u0434\u044f \u0438\u0437 IDE<\/strong>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizer\">AddressSanitizer<\/a>, \u043e\u0434\u0438\u043d \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"bash\">gcc -fsanitize=address -g your_program.c<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043f\u0440\u043e\u0439\u0442\u0438 \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<h4>\u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0438\u0447\u0438 \u0438\u0437 C++ (\u0447\u0442\u043e?)<\/h4>\n<p>\u041a\u0430\u043a \u0432\u044b, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0437\u043d\u0430\u0435\u0442\u0435, RAII \u2014 \u044d\u0442\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0438\u0437 C++, \u0433\u0434\u0435 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0440\u0435\u0441\u0443\u0440\u0441 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0430 \u043f\u0440\u0438 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f. &#171;\u041d\u043e \u0441\u0442\u043e\u043f,&#187; \u2014 \u0441\u043a\u0430\u0436\u0435\u0442\u0435 \u0432\u044b, \u2014 &#171;\u0432 C \u043d\u0435\u0442 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u043e\u0432!&#187;. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c RAII \u0434\u043b\u044f C, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0447\u0438\u0441\u0442\u043a\u0438.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdlib.h>  typedef struct {     int* array;     size_t size; } IntArray;  IntArray* IntArray_create(size_t size) {     IntArray* ia = malloc(sizeof(IntArray));     if (ia) {         ia->array = malloc(size * sizeof(int));         ia->size = size;     }     return ia; }  void IntArray_destroy(IntArray* ia) {     if (ia) {         free(ia->array);         free(ia);     } }<\/code><\/pre>\n<p><code>IntArray_create<\/code> \u0438 <code>IntArray_destroy<\/code> \u0438\u0433\u0440\u0430\u044e\u0442 \u0440\u043e\u043b\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0438 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430. <\/p>\n<p>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u2014 \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0441\u043a\u0440\u044b\u0432\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0412 C \u044d\u0442\u043e \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b:<\/p>\n<pre><code class=\"cpp\">typedef struct {     FILE* file; } FileResource;  FileResource* FileResource_open(const char* filename, const char* mode) {     FileResource* fr = malloc(sizeof(FileResource));     if (fr) {         fr->file = fopen(filename, mode);     }     return fr; }  void FileResource_close(FileResource* fr) {     if (fr) {         fclose(fr->file);         free(fr);     } }<\/code><\/pre>\n<h4>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c<\/h4>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pthreads#:~:text=In%20computing%2C%20POSIX%20Threads%2C%20commonly,work%20that%20overlap%20in%20time.\">pthreads<\/a>, \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0442\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d:<\/p>\n<pre><code class=\"cpp\">#include &lt;pthread.h> #include &lt;stdio.h> #include &lt;stdlib.h>  \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 void* thread_function(void* arg) {     printf(\"\u041f\u0440\u0438\u0432\u0435\u0442 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u0430! \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: %s\\n\", (char*)arg);     return NULL; }  int main() {     pthread_t thread_id;     char* message = \"Thread's Message\";      \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a     if(pthread_create(&amp;thread_id, NULL, thread_function, (void*)message)) {         fprintf(stderr, \"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\\n\");         return 1;     }      \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u0430     if(pthread_join(thread_id, NULL)) {         fprintf(stderr, \"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\\n\");         return 2;     }      printf(\"\u041f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0440\u0430\u0431\u043e\u0442\u0443\\n\");     return 0; }<\/code><\/pre>\n<p><code>pthread_create<\/code> \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 <code>thread_function<\/code>, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a <code>pthread_join<\/code> \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<p>\u0420\u0430\u0437\u0434\u0435\u043b\u0438\u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0447\u0430\u0441\u0442\u0438 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441. \u0421\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"cpp\">#include &lt;pthread.h> #include &lt;stdio.h> #include &lt;stdlib.h>  #define SIZE 1000000 #define THREADS 4  int array[SIZE]; long long sum[THREADS] = {0};  void* sum_function(void* arg) {     int thread_part = (int)arg;     int start = thread_part * (SIZE \/ THREADS);     int end = (thread_part + 1) * (SIZE \/ THREADS);      for(int i = start; i &lt; end; i++) {         sum[thread_part] += array[i];     }      return NULL; }  int main() {     pthread_t threads[THREADS];      \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430     for(int i = 0; i &lt; SIZE; i++) {         array[i] = i + 1;     }      \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0434\u043b\u044f \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u0435\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\u0430     for(int i = 0; i &lt; THREADS; i++) {         pthread_create(&amp;threads[i], NULL, sum_function, (void*)i);     }      \/\/ \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432     for(int i = 0; i &lt; THREADS; i++) {         pthread_join(threads[i], NULL);     }      \/\/ \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432     long long total_sum = 0;     for(int i = 0; i &lt; THREADS; i++) {         total_sum += sum[i];     }      printf(\"\u041e\u0431\u0449\u0430\u044f \u0441\u0443\u043c\u043c\u0430: %lld\\n\", total_sum);     return 0; }<\/code><\/pre>\n<p>\u041a\u043e\u0434 \u0434\u0435\u043b\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0447\u0435\u0442\u044b\u0440\u0435 \u0447\u0430\u0441\u0442\u0438 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0441\u0443\u043c\u043c\u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<h4>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/h4>\n<p><abbr class=\"habraabbr\" title=\"\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043b\u0438\u0431\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0438\u043a\u043e\u043c, \u043b\u0438\u0431\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0432\u0441\u0435; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430\" data-title=\"&lt;p>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430&#187; data-title=&#187;&lt;p>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043b\u0438\u0431\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0438\u043a\u043e\u043c, \u043b\u0438\u0431\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0432\u0441\u0435; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0438 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430  &lt;\/p>&#187; data-abbr=&#187;\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438&#187;>\u0410\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/abbr> \u0447\u0430\u0441\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0439, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 C11 \u0438\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430\u043c\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 GCC.<\/p>\n<p>\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C11 \u0432\u0432\u0435\u043b \u044f\u0432\u043d\u0443\u044e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0443\u043b\u044c <code>&lt;stdatomic.h><\/code>. \u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdatomic.h> #include &lt;stdio.h> #include &lt;pthread.h>  atomic_int counter = ATOMIC_VAR_INIT(0);  void* increment(void* arg) {     for (int i = 0; i &lt; 100000; ++i) {         atomic_fetch_add_explicit(&amp;counter, 1, memory_order_relaxed);     }     return NULL; }  int main() {     pthread_t t1, t2;      if(pthread_create(&amp;t1, NULL, increment, NULL)) {         return 1;     }     if(pthread_create(&amp;t2, NULL, increment, NULL)) {         return 1;     }      pthread_join(t1, NULL);     pthread_join(t2, NULL);      printf(\"\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430: %d\\n\", atomic_load(&amp;counter));     return 0; }<\/code><\/pre>\n<p><code>atomic_int<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430, \u0430 <code>atomic_fetch_add_explicit<\/code> \u2014 \u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. <code>memory_order_relaxed<\/code> \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0434\u0440\u0443\u0433\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c, \u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0441\u0442\u044c \u0441\u0430\u043c\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p><a href=\"https:\/\/gcc.gnu.org\/\">GCC<\/a> \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 C, \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 C11, \u0438\u043b\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 <code>&lt;stdatomic.h><\/code> \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0438\u043b\u0438 \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u0430.<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u0439 \u043e\u0431\u043c\u0435\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h> #include &lt;pthread.h>  int counter = 0;  void* increment(void* arg) {     for (int i = 0; i &lt; 100000; ++i) {         __sync_fetch_and_add(&amp;counter, 1);     }     return NULL; }  int main() {     pthread_t t1, t2;      if(pthread_create(&amp;t1, NULL, increment, NULL)) {         return 1;     }     if(pthread_create(&amp;t2, NULL, increment, NULL)) {         return 1;     }      pthread_join(t1, NULL);     pthread_join(t2, NULL);      printf(\"\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430: %d\\n\", counter);     return 0; }<\/code><\/pre>\n<p>\u0424\u043d\u043a\u0446\u0438\u044f <code>__sync_fetch_and_add<\/code> \u0438\u0437 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 GCC \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f\u0434\u043b\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0433\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430. <\/p>\n<hr\/>\n<p>\u0412 \u0421 \u0435\u0441\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u0430 \u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0441\u0432\u043e\u0438\u043c \u043a\u043e\u0434\u043e\u043c, \u043d\u043e \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u044d\u0442\u0438\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0438 <strong>\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c<\/strong>. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u044d\u0442\u0443 \u0441\u0432\u043e\u0431\u043e\u0434\u0443 \u0441 \u0443\u043c\u043e\u043c, \u043e\u0441\u043e\u0437\u043d\u0430\u0432\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f \u0441\u0432\u043e\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439. <\/p>\n<p><a href=\"https:\/\/otus.pw\/QZ1K\/\">\u041f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a> \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/QZ1K\/\">&#171;\u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0421&#187;<\/a> \u043f\u0440\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0421 \u0438 Python.<\/p>\n<\/p>\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\/800381\/\"> https:\/\/habr.com\/ru\/articles\/800381\/<\/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-370849","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/370849","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=370849"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/370849\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}