{"id":413780,"date":"2024-06-29T23:20:37","date_gmt":"2024-06-29T23:20:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=413780"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=413780","title":{"rendered":"<span>PVS-Studio \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0445 \u0440\u0430\u0431\u043e\u0442 \u043d\u0430 C \u0438 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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/dc\/yf\/mj\/dcyfmjbguezmsw8z33qdtvn8b8y.png\" alt=\"C++ error: &quot;pointer being freed was not allocated\" data-src=\"https:\/\/habrastorage.org\/webt\/dc\/yf\/mj\/dcyfmjbguezmsw8z33qdtvn8b8y.png\"\/><\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u0442\u0440\u0435\u0442\u0438\u043b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u043d\u0430 Stack Overflow \u043e\u0442 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u0438\u0437\u0443\u0447\u0430\u044e\u0449\u0435\u0433\u043e \u044f\u0437\u044b\u043a C++. \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f PVS-Studio. \u0427\u0435\u043b\u043e\u0432\u0435\u043a \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442, \u043d\u0435 \u043e\u0442\u0432\u043b\u0435\u043a\u0430\u044f \u0434\u0440\u0443\u0433\u0438\u0445.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u042f \u0443\u0436\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b <a href=\"https:\/\/habr.com\/ru\/company\/pvs-studio\/blog\/673788\/\">\u0437\u0434\u0435\u0441\u044c<\/a>, \u0447\u0442\u043e online \u0432\u0435\u0440\u0441\u0438\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 PVS-Studio \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u0442\u044c \u0436\u0438\u0437\u043d\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439.<\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u0433\u043b\u044f\u043d\u0435\u043c \u0432 \u0434\u0438\u0441\u043a\u0443\u0441\u0441\u0438\u044e &#171;<a href=\"https:\/\/stackoverflow.com\/questions\/72511624\/c-error-pointer-being-freed-was-not-allocated\">C++ error: &#171;pointer being freed was not allocated<\/a>&#187; \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 Stack Overflow \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdexcept> #include &lt;iostream> #include &lt;vector> #include &lt;algorithm> #include &lt;numeric>  using std::cout; using std::endl; using std::vector;        typedef vector&lt;int> ints;  void print_ints(vector&lt;int>); void print_ints_vec(vector&lt;vector&lt;int>>); void int_part(int, vector&lt;vector&lt;int>>&amp;);  int main()  {   vector&lt;vector&lt;int>> partition;   int_part(5, partition);   print_ints_vec(partition);    return 0; }  void int_part(int sum, vector&lt;vector&lt;int>>&amp; res) {   vector&lt;int> init_xs = vector&lt;int>{sum};   vector&lt;int>* xs = &amp;init_xs; \/\/ POINTER INITIALIZED TO vector&lt;int>   int current_sum = sum;    while (true)    {     current_sum = accumulate(xs->begin(), xs->end(), 0);      if (current_sum == sum)     {       res.push_back(*xs);       vector&lt;int> next_xs;       vector&lt;int>::iterator it = find(xs->begin(), xs->end(), 1);       if (it == xs->begin()) return;       copy(xs->begin(), it, back_inserter(next_xs));       next_xs[next_xs.size() - 1] -= 1;       xs = &amp;next_xs; \/\/ POINTER REASSIGNED TO ANOTHER vector&lt;int>     }     else      {       int tail = xs->back();       int diff = sum - current_sum;       int m = std::min(tail, sum - tail);       int next_tail = current_sum + m > sum ? diff : m;       xs->push_back(next_tail);     }   } }  void print_ints(ints v) \/\/ PRINT UTILITY {   cout &lt;&lt; \"[ \";   for (const int&amp; n : v) { cout &lt;&lt; n &lt;&lt; \"; \"; }   cout &lt;&lt; \"]\" &lt;&lt; endl; }  void print_ints_vec(vector&lt;ints> v) \/\/ PRINT UTILITY {   cout &lt;&lt; \"[ \\n\";   for (const vector&lt;int>&amp; xs : v) { cout &lt;&lt; \"  \"; print_ints(xs); }   cout &lt;&lt; \"]\" &lt;&lt; endl; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0433\u043b\u0430\u0441\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0438 \u0441\u0438\u043b\u044b \u043d\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b, \u0432\u044b\u0438\u0441\u043a\u0438\u0432\u0430\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438. \u0418 \u043d\u0435 \u043d\u0430\u0434\u043e! \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u043f\u043e\u0440\u0443\u0447\u0438\u043c \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0443 PVS-Studio.<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0447\u0442\u043e \u043e\u043d <a href=\"https:\/\/godbolt.org\/z\/9e61K7hTq\">\u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442<\/a>: V506 Pointer to local variable &#8216;next_xs&#8217; is stored outside the scope of this variable. Such a pointer will become invalid. <\/p>\n<p>  <\/p>\n<p>\u0421\u0442\u0440\u043e\u043a\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">xs = &amp;next_xs; \/\/ POINTER REASSIGNED TO ANOTHER vector&lt;int><\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0448\u0438\u0431\u043a\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0440\u0443\u0448\u0435\u043d. \u041d\u0430 \u044d\u0442\u0443 \u043e\u0448\u0438\u0431\u043a\u0443 \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043b\u044e\u0434\u0438 \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u043b\u0438, \u0432 \u0447\u0451\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u0435\u0437 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0442 \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u0433. \u0412\u0435\u0434\u044c \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432\u043f\u043e\u043b\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430 PVS-Studio \u043f\u043e \u0434\u0438\u0430\u0433\u043d\u043e\u0441\u0442\u0438\u043a\u0435 <a href=\"https:\/\/pvs-studio.com\/ru\/docs\/warnings\/v506\/\">V506<\/a>.<\/p>\n<p>  <\/p>\n<h2 id=\"vyvod\">\u0412\u044b\u0432\u043e\u0434<\/h2>\n<p>  <\/p>\n<p>\u0418\u043c\u0435\u0439\u0442\u0435 \u0432 \u0432\u0438\u0434\u0443 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f PVS-Studio \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. \u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u0432\u044b\u0434\u0430\u0432\u0430\u0435\u043c\u0430\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u043c, \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0432 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438, \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0443, \u0447\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0441 \u0435\u0433\u043e \u043a\u043e\u0434\u043e\u043c. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u043d\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u043e\u0431\u0437\u043e\u0440\u043e\u0432 \u043a\u043e\u0434\u0430 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043b\u044e\u0434\u044c\u043c\u0438. \u0412\u0435\u0434\u044c \u0447\u0435\u043b\u043e\u0432\u0435\u043a \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0439\u0434\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u043d\u043e \u0434\u0430\u0441\u0442 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438, \u043a\u0430\u043a \u043b\u0443\u0447\u0448\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a \u043f\u0440\u0438 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e.<\/p>\n<p>  <\/p>\n<p><strong>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438:<\/strong><\/p>\n<p>  <\/p>\n<ol>\n<li><a href=\"https:\/\/pvs-studio.com\/ru\/blog\/terms\/0046\/\">\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u043a\u043e\u0434\u0430<\/a>.<\/li>\n<li>PVS-Studio: <a href=\"https:\/\/pvs-studio.com\/ru\/pvs-studio\/godbolt\/\">online \u0432\u0435\u0440\u0441\u0438\u044f<\/a>.<\/li>\n<li>PVS-Studio: <a href=\"https:\/\/pvs-studio.com\/ru\/order\/for-students\/\">\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432<\/a>.<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435\u0439 \u0441 \u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u0439 \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u0435\u0439, \u0442\u043e \u043f\u0440\u043e\u0448\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043f\u0435\u0440\u0435\u0432\u043e\u0434: Andrey Karpov. <a href=\"https:\/\/pvs-studio.com\/en\/blog\/posts\/cpp\/0962\/\">PVS-Studio to help with schoolwork-like tasks in C and C++<\/a>.<\/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\/675380\/\"> https:\/\/habr.com\/ru\/articles\/675380\/<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/dc\/yf\/mj\/dcyfmjbguezmsw8z33qdtvn8b8y.png\" alt=\"C++ error: &quot;pointer being freed was not allocated\" data-src=\"https:\/\/habrastorage.org\/webt\/dc\/yf\/mj\/dcyfmjbguezmsw8z33qdtvn8b8y.png\"\/><\/p>\n<p>  <\/p>\n<p>\u0412\u0441\u0442\u0440\u0435\u0442\u0438\u043b \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u043d\u0430 Stack Overflow \u043e\u0442 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u0438\u0437\u0443\u0447\u0430\u044e\u0449\u0435\u0433\u043e \u044f\u0437\u044b\u043a C++. \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f PVS-Studio. \u0427\u0435\u043b\u043e\u0432\u0435\u043a \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442, \u043d\u0435 \u043e\u0442\u0432\u043b\u0435\u043a\u0430\u044f \u0434\u0440\u0443\u0433\u0438\u0445.<\/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-413780","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/413780","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=413780"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/413780\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=413780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=413780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=413780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}