{"id":4570,"date":"2014-03-30T13:33:57","date_gmt":"2014-03-30T13:33:57","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-do-i-have-to-garbage-collect-in-a-c-destructor-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:42:27","modified_gmt":"2022-08-30T15:42:27","slug":"what-do-i-have-to-garbage-collect-in-a-c-destructor-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-do-i-have-to-garbage-collect-in-a-c-destructor-collection-of-common-programming-errors\/","title":{"rendered":"What do I have to garbage collect in a C++ destructor-Collection of common programming errors"},"content":{"rendered":"<p>You must <code>delete<\/code> every pointer you allocate with <code>new<\/code>. It&#8217;s that simple, and it&#8217;s that complicated; you must not lose track of any pointer allocated by <code>new<\/code> until you have <code>delete<\/code>d it.<\/p>\n<p>Also, you need to make sure that if you use <code>new[]<\/code> to allocate a pointer, you call <code>delete[]<\/code> to deallocate it.<\/p>\n<p>It&#8217;s not about what pointers you happen to have in a class instance. You need to know who <em>owns<\/em> them (the owner is the one responsible for deleting them). If your object owns those pointers, then it should delete them. If it doesn&#8217;t own them, then it shouldn&#8217;t.<\/p>\n<p>This is also why experienced C++ programmers avoid naked pointers where possible. Smart pointers allow you to express ownership types semantically in the language. That way, you don&#8217;t have to keep track of who owns what; you know who owns it by the type of smart pointer being used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You must delete every pointer you allocate with new. It&#8217;s that simple, and it&#8217;s that complicated; you must not lose track of any pointer allocated by new until you have deleted it. Also, you need to make sure that if you use new[] to allocate a pointer, you call delete[] to deallocate it. It&#8217;s not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-4570","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4570","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=4570"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4570\/revisions"}],"predecessor-version":[{"id":8784,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4570\/revisions\/8784"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}