{"id":2744,"date":"2022-08-30T15:27:25","date_gmt":"2022-08-30T15:27:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/why-doesnt-c-give-compiler-error-when-you-call-delete-on-a-non-pointer-stack-allocated-variable-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:25","modified_gmt":"2022-08-30T15:27:25","slug":"why-doesnt-c-give-compiler-error-when-you-call-delete-on-a-non-pointer-stack-allocated-variable-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-doesnt-c-give-compiler-error-when-you-call-delete-on-a-non-pointer-stack-allocated-variable-closed-collection-of-common-programming-errors\/","title":{"rendered":"Why doesn&#39;t C++ give compiler error when you call delete on a non-pointer stack allocated variable? [closed]-Collection of common programming errors"},"content":{"rendered":"<p>If you are thinking of something like this:<\/p>\n<pre><code>int a;\nint *pa = &amp;a;\ndelete pa;\n<\/code><\/pre>\n<p>Then you won&#8217;t get a compiler error because the above code, while wrong, is not illegal by language definition. However, the compiler may issue a warning if the case is obvious enough (I didn&#8217;t try, but I guess it won&#8217;t). A static analyzer tool should detect this though.<\/p>\n<p>If you have something like this, though, there is no way the compiler would know that something is wrong:<\/p>\n<pre><code>void foo(int* pa)\n{\n    delete pa;\n}\n...\nint a;\nfoo(&amp;a);\n<\/code><\/pre>\n<p>To detect this, you&#8217;ll definitely need a good static analyzer such as Coverity. But generally you should avoid patterns like this. Deleting a pointer should always be the responsibility of the class (or other entity) that created it. Even better, you should use smart pointers instead of calling new\/delete manually (you&#8217;ll need a C++11 compatible compiler or use Boost).<\/p>\n<p id=\"rop\"><small>Originally posted 2014-02-08 15:51:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>If you are thinking of something like this: int a; int *pa = &amp;a; delete pa; Then you won&#8217;t get a compiler error because the above code, while wrong, is not illegal by language definition. However, the compiler may issue a warning if the case is obvious enough (I didn&#8217;t try, but I guess it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2744","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2744"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2744\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}