{"id":94,"date":"2010-12-31T18:57:00","date_gmt":"2010-12-31T18:57:00","guid":{"rendered":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/"},"modified":"2010-12-31T18:57:00","modified_gmt":"2010-12-31T18:57:00","slug":"delete-pointer-passed-to-function-c-parameter","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/","title":{"rendered":"Delete Pointer Passed to Function C++ Parameter"},"content":{"rendered":"<p>I hate C++ pointers. I switch from C to C++ to Java a lot, so I tend to need refreshing once in a while when I go from Java to C++.<br \/>\nI was updating some old code the other day and came across a function which was supposed to delete a pointer passed to it and then set it to null. When the function returned, the pointer was deleted, but was not null. Reason why: the function had been set up wrong:<\/p>\n<p>void deletePointer(XYZ* _xyz)<br \/>\n{<br \/>\n  delete _xyz;<br \/>\n  _xyz = 0;<br \/>\n}<\/p>\n<p>\nvoid callDeletePointer()<br \/>\n{<br \/>\n  XYZ* xyz = new XYZ();<br \/>\n  &#8230;<br \/>\n  &#8230;<br \/>\n  &#8230;<br \/>\n  deletePointer(xyz);<br \/>\n}<\/p>\n<p>The way deletePointer really needed to be set up is like this:<\/p>\n<p>void deletePointer(<b>XYZ*&#038; _xyz<\/b>)<br \/>\n{<br \/>\n  delete _xyz;<br \/>\n  _xyz = 0;<br \/>\n}<\/p>\n<p>Here&#8217;s why:<br \/>\nThe first deletePointer receives a copy of the pointer to _xyz, which allows the user to modify the object addressed by the pointer. <br \/>\nThe second deletePointer receives the actual pointer to _xyz, which allows the user to modify the pointer itself.<\/p>\n<p>Oh and one more tip:<br \/>\nWhen deallocating memory do this: <\/p>\n<p>XYZ* xyz = new XYZ();<br \/>\ndelete xyz;<br \/>\nxyz = 0;<\/p>\n<p>instead of:<\/p>\n<p>XYZ* xyz = new XYZ();<br \/>\n<b>if(xyz != 0)<br \/>\n{<br \/>\n  delete xyz;<br \/>\n  xyz = 0;<br \/>\n}<\/b><\/p>\n<p>Apparently, C++ guarantees operator delete() won&#8217;t be called by a delete if the pointer is 0, so testing for null is not necessary. In fact, if you do include a pointer null test, the test will be done twice.<\/p>\n<p>Source: <a href=\"http:\/\/www.amazon.com\/gp\/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26redirect%3Dtrue%26ref_%3Dnb_sb_noss%26rh%3Dn%253A283155%252Cn%253A%25211000%252Cn%253A5%252Ck%253AC%252B%252B%2520primer%2520third%26field-keywords%3DC%252B%252B%2520primer%2520third%26url%3Dnode%253D5%26ajr%3D4&amp;tag=extcas-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=390957\">C++ Primer, Third Edition, by Stanley B. Lippman, Josee Lajoie<\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><\/p>\n<p>\n<a href=\"http:\/\/www.amazon.com\/gp\/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26ref_%3Dnb_sb_noss%26rh%3Dn%253A283155%252Ck%253AC%252B%252B%26field-keywords%3DC%252B%252B%26url%3Dsearch-alias%253Dstripbooks%26ajr%3D4&amp;tag=extcas-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=390957\">More C++<\/a><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><\/p>\n<p>\n<iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=extcas-20&#038;o=1&#038;p=8&#038;l=bpl&#038;asins=0201721481&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"align:left;padding-top:5px;width:131px;height:245px;padding-right:10px;\"align=\"left\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=extcas-20&#038;o=1&#038;p=8&#038;l=bpl&#038;asins=0672326973&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"align:left;padding-top:5px;width:131px;height:245px;padding-right:10px;\"align=\"left\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hate C++ pointers. I switch from C to C++ to Java a lot, so I tend to need refreshing once in a while when I go from Java to C++. I was updating some old code the other day and came across a function which was supposed to delete a pointer passed to it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[171,172],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-delete-pointer-passed-to-function-c-parameter","category-set-pointer-to-null-through-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Delete Pointer Passed to Function C++ Parameter - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Delete Pointer Passed to Function C++ Parameter - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"I hate C++ pointers. I switch from C to C++ to Java a lot, so I tend to need refreshing once in a while when I go from Java to C++. I was updating some old code the other day and came across a function which was supposed to delete a pointer passed to it [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2010-12-31T18:57:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1\" \/>\n<meta name=\"author\" content=\"zofxare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"zofxare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/\"},\"author\":{\"name\":\"zofxare\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\"},\"headline\":\"Delete Pointer Passed to Function C++ Parameter\",\"datePublished\":\"2010-12-31T18:57:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/\"},\"wordCount\":247,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=ur2&amp;o=1\",\"articleSection\":[\"Delete Pointer Passed to Function C++ Parameter\",\"Set pointer to null through function\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/\",\"name\":\"Delete Pointer Passed to Function C++ Parameter - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=ur2&amp;o=1\",\"datePublished\":\"2010-12-31T18:57:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=ur2&amp;o=1\",\"contentUrl\":\"https:\\\/\\\/www.assoc-amazon.com\\\/e\\\/ir?t=extcas-20&amp;l=ur2&amp;o=1\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2010\\\/12\\\/31\\\/delete-pointer-passed-to-function-c-parameter\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Delete Pointer Passed to Function C++ Parameter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/094f5dde70100b9f8fbc63174a2a40ce\",\"name\":\"zofxare\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/?s=96&d=retro&r=g\",\"caption\":\"zofxare\"},\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/zofxare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Delete Pointer Passed to Function C++ Parameter - Zofxare Blog Home","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/","og_locale":"en_US","og_type":"article","og_title":"Delete Pointer Passed to Function C++ Parameter - Zofxare Blog Home","og_description":"I hate C++ pointers. I switch from C to C++ to Java a lot, so I tend to need refreshing once in a while when I go from Java to C++. I was updating some old code the other day and came across a function which was supposed to delete a pointer passed to it [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/","og_site_name":"Zofxare Blog Home","article_published_time":"2010-12-31T18:57:00+00:00","og_image":[{"url":"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1","type":"","width":"","height":""}],"author":"zofxare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"zofxare","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/"},"author":{"name":"zofxare","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce"},"headline":"Delete Pointer Passed to Function C++ Parameter","datePublished":"2010-12-31T18:57:00+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/"},"wordCount":247,"commentCount":0,"image":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#primaryimage"},"thumbnailUrl":"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1","articleSection":["Delete Pointer Passed to Function C++ Parameter","Set pointer to null through function"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/","name":"Delete Pointer Passed to Function C++ Parameter - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#primaryimage"},"image":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#primaryimage"},"thumbnailUrl":"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1","datePublished":"2010-12-31T18:57:00+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#primaryimage","url":"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1","contentUrl":"https:\/\/www.assoc-amazon.com\/e\/ir?t=extcas-20&amp;l=ur2&amp;o=1"},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2010\/12\/31\/delete-pointer-passed-to-function-c-parameter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"Delete Pointer Passed to Function C++ Parameter"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/094f5dde70100b9f8fbc63174a2a40ce","name":"zofxare","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=retro&r=g","caption":"zofxare"},"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/zofxare\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":0,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}